用內容初始化核心數據數據庫的最佳方式是什麼?我的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