用内容初始化核心数据数据库的最佳方式是什么?我的iPhone应用程序将有一个静态数据库与产品(数据和图像)。如何/在哪里存储图像,如何预先填充数据库?iPhone核心数据预设
3
A
回答
1
这里是我做过什么:
- 创建数据库的iPhone应用程序内
- 我创建在Xcode中的模型,做对数据库的查询(这将创建数据库)
- 我静数据是一个CSV文件
- 使用Ruby脚本读取CSV文件
- 使用红宝石sqlite3的宝石将数据插入到数据库
- 复制回项目
备选:
- 存储包含应用内数据的CSV/XML文件
- 解析它在启动和创建NSMAnagedObjects
工具/资源:
- Base s oftware编辑/查看sqlite3的数据库
数据库位置: 我怕我不记得它放在我的头顶,但是当你使用模拟器应用程序将建成并复制到目录。我认为你的应用程序的路径是这样的。数据库取决于它如何设置通常在Documents文件夹中。
~User/Library/Application Settings/iOS Simulator/<version>/<app id>/
0
参考苹果官方文档提供了一种预填充数据到核心数据: 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;
}
希望这个作品,祝你好运!
相关问题
- 1. 核心数据iPhone
- 2. 核心数据错误iPhone
- 3. iPhone核心数据关系
- 4. iPhone SDK:核心数据
- 5. iPhone核心数据入门?
- 6. iPhone - 核心数据崩溃
- 7. iphone核心数据质疑
- 8. iPhone核心数据问题
- 9. iPhone核心数据查询
- 10. iPhone - 核心数据问题?
- 11. iPhone - 核心数据问题?
- 12. iPhone和核心数据
- 13. iPhone - 核心数据迁移
- 14. iPhone核心数据错误?
- 15. iphone核心数据 - 为什么核心数据如此之慢?
- 16. iPhone:核心数据更新数据
- 17. iPhone - 核心数据中获取数据
- 18. 设置与核心数据
- 19. 预填充.sqlite的核心数据(Swift3)
- 20. Swift核心数据预加载persistentStoreCoordinator:
- 21. 预填充版本的核心数据?
- 22. iPhone核心数据可以缓存NSManagedObjects?
- 23. iPhone核心数据模拟器
- 24. iPhone核心数据不刷新表
- 25. 核心数据iPhone - 加载字符串
- 26. iPhone核心数据递归关系
- 27. iPhone核心数据内部不一致
- 28. iPhone核心数据和多线程
- 29. iPhone核心数据的问题
- 30. 学习iPhone核心数据的来源
我该如何复制数据库?数据库在模拟器上创建。你如何将这个数据库打包到你的appstore的最终应用程序中? – xpepermint 2011-02-09 15:48:19