gpt4 book ai didi

iphone - 将现有数据预加载到基于 Core Data 的 iPhone 应用程序中?

转载 作者:行者123 更新时间:2023-12-03 18:41:30 24 4
gpt4 key购买 nike

在之前的项目中,我为 2.2.x 构建了一个使用 SQLite 的 iPhone 应用程序。它具有需要预先加载到构建中的现有数据(XML 形式)。所以我写了一个小工具,使用 libxml2 解析 XML,然后编写一个 SQLite 数据库,然后将其作为资源直接包含在构建中。这效果很好。

我将在几周内为不同的客户启动一个新项目,该项目的参数基本相同。我将需要解析一些现有数据并将其转储到应用程序将显示的文件中。不过,这一次我想使用 Core Data 并为 3.x 设备构建应用程序。但是,我无法显式直接访问 Core Data 使用的底层数据库架构。 (这就是核心数据的要点)

如何将现有数据预加载到基于 Core Data 的 iPhone 应用程序中?我可以自动化该过程(类似于我上面使用 SQLite 所做的事情)吗?

最佳答案

我参加这个聚会有点晚了,但我正在为 GroceryList 做类似的事情。我的 CoreData sqlite 存储中需要将数据存储在 plist 文件中。我编写了一个在 Mac 上运行的命令行基础工具,它解析 plist 文件,然后使用我的 Core Data 对象模型创建一个 sqlite 存储。我运行这个命令行工具作为我的构建的一部分(对于某些构建配置),以便我可以随意重新生成数据。要在 xcode 中创建此类工具,请选择"file"->“新建项目”->“Mac OS X”->“命令行工具”,然后从“类型”菜单中选择“核心数据”。这是一些示例代码:

#import <objc/objc-auto.h>

int main (int argc, const char * argv[]) {

objc_startCollectorThread();

//You may not know this, but NSUserDefaults can be used to parse command line arguments!
//in this case, the arguments are passed in like this:
// -fullMomPath /hd/some/path/file.mom -fullStorePath /hd/some/path/file.sql
//by passing in the storePath, the calling script knows where the sqlite file will be and can copy it to the resulting application bundle
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSString *momPath = [args stringForKey:@"fullMomPath"];
NSString *storePath = [args stringForKey:@"fullStorePath"];

// Create the managed object context
NSManagedObjectContext *context = managedObjectContext(momPath, storePath);


//build and save your NSManagedObjects here
//in my case, i parse some plist files and create GroceryList type stuff, but whatever you do is your business.

return 0;
}

NSManagedObjectModel *managedObjectModel(NSString* momPath) {

static NSManagedObjectModel *model = nil;

if (model != nil) {
return model;
}

NSURL *modelURL = [NSURL fileURLWithPath:momPath];
model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

return model;
}



NSManagedObjectContext *managedObjectContext(NSString* momPath, NSString* storePath) {

static NSManagedObjectContext *context = nil;
if (context != nil) {
return context;
}

context = [[NSManagedObjectContext alloc] init];

NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel(momPath)];
[context setPersistentStoreCoordinator: coordinator];

NSString *STORE_TYPE = NSSQLiteStoreType;

NSURL *url = [NSURL fileURLWithPath:storePath];

NSError *error;
NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];

if (newStore == nil) {
NSLog(@"Store Configuration Failure\n%@",
([error localizedDescription] != nil) ?
[error localizedDescription] : @"Unknown Error");
}

return context;
}

关于iphone - 将现有数据预加载到基于 Core Data 的 iPhone 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1767322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com