iPhone核心数据预设
Posted
技术标签:
【中文标题】iPhone核心数据预设【英文标题】:iPhone core data preset 【发布时间】:2011-02-09 14:12:04 【问题描述】:用内容初始化 Core Data 数据库的最佳方法是什么。我的 iPhone 应用程序将有一个包含产品(数据和图像)的静态数据库。如何/在哪里存储图像,如何预填充数据库?
【问题讨论】:
【参考方案1】:这是我所做的:
在 iPhone 应用程序中创建数据库 我在 XCode 中创建了模型并对数据库进行了查询(这将创建数据库) 我的静态数据是 CSV 文件 使用 Ruby 脚本读取 CSV 文件 使用 ruby gem sqlite3 将数据插入数据库 复制回项目中替代方案:
将包含数据的 CSV/XML 文件存储在应用程序中 在启动时解析它并创建您的 NSManagedObjects工具/资源:
Base 用于编辑/查看 sqlite3 数据库的软件数据库位置: 恐怕我不记得它了,但是当您使用模拟器时,您的应用程序将被构建并复制到一个目录中。我认为您的应用程序的路径将是这样的。取决于其设置方式的数据库通常位于 Documents 文件夹中。
~User/Library/Application Settings/ios Simulator/<version>/<app id>/
【讨论】:
我该如何复制数据库?数据库是在模拟器上创建的。您如何将此数据库打包到应用商店的最终应用中? @xpepermint - 更新了我的帖子,但不记得我头顶的确切位置,但希望这足够接近【参考方案2】:参考 Apple 官方文档提供了一种将数据预填充到 Core Data 中的方法: Core Data Books
在我自己的应用程序中,我用这个替换了 AppDeledate 中的函数“NSPersistentStoreCoordinator”:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
if (_persistentStoreCoordinator != nil)
return _persistentStoreCoordinator;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataBooks.CDBStore"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:[storeURL path]])
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"CoreDataBooks" withExtension:@"CDBStore"];
if (defaultStoreURL)
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSError *error;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
return _persistentStoreCoordinator;
希望这行得通,祝你好运!
【讨论】:
以上是关于iPhone核心数据预设的主要内容,如果未能解决你的问题,请参考以下文章