我正在制作一个快速的应用程序,我需要预先加载核心数据。为此,我创建了另一个示例iOS应用程序,它使用与主应用程序完全相同的数据模型。此示例创建包含数据的sqlite文件。我将示例应用程序创建的sqlite文件(.sqlite和2 .sqlite- *)与数据复制到主应用程序的资源包中,并将我的代码复制到" persistentStoreCoordinator"如下所示。
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url: NSURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("myAssistant.sqlite")
//----------------------------------------------------------------------------
// Add the below code
// Check if a data store already exists in the documents directory.
var checker: Bool = NSFileManager.defaultManager().fileExistsAtPath(url.path!)
if (!checker){
// If there’s no Data Store present (which is the case when the app first launches), identify the sqlite file we added in the Bundle Resources, copy it into the Documents directory, and make it the Data Store.
var sqlitePath: NSString = NSBundle.mainBundle().pathForResource("myAssistant", ofType: "sqlite")!
NSFileManager.defaultManager().copyItemAtPath(sqlitePath, toPath: url.path!, error: nil)
}
//----------------------------------------------------------------------------/
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this 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.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
但是,主应用程序崩溃时出现以下错误。
2015-02-19 18:23:37.235 myAssistant[10477:488435] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/NishiJain/Library/Developer/CoreSimulator/Devices/428084BE-0E2B-4F58-A274-AED2C76845DF/data/Containers/Data/Application/7F478A95-CD1E-4C99-BF62-60DAA39C6C11/Documents/myAssistant.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7f9c11e4e760 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes = {
Category = <be1d87d2 e4c72a88 a830fb54 b9aea852 7887b5a6 bf2f85bb 22e878d0 c6916d06>;
ListDetails = <2cc64684 6520b473 d4bc4ae5 39f66fa0 0437788e ef1a0d91 1bdd7b52 86c0fbf2>;
Lists = <c2491da1 58622f56 64112d31 ba3e294a 1167e3f1 5427cd3a a56c1613 e2c0ee8a>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
""
);
NSStoreType = SQLite;
NSStoreUUID = "F84E0B5C-F83B-48ED-9043-5F9B6752FCA4";
"_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
metadata = {
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes = {
Category = <be1d87d2 e4c72a88 a830fb54 b9aea852 7887b5a6 bf2f85bb 22e878d0 c6916d06>;
ListDetails = <2cc64684 6520b473 d4bc4ae5 39f66fa0 0437788e ef1a0d91 1bdd7b52 86c0fbf2>;
Lists = <c2491da1 58622f56 64112d31 ba3e294a 1167e3f1 5427cd3a a56c1613 e2c0ee8a>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
""
);
NSStoreType = SQLite;
NSStoreUUID = "F84E0B5C-F83B-48ED-9043-5F9B6752FCA4";
"_NSAutoVacuumLevel" = 2;
};
reason = "The model used to open the store is incompatible with the one used to create the store";
}
2015-02-19 18:23:37.243 myAssistant[10477:488435] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x7f9c11f27600 {NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSLocalizedDescription=Failed to initialize the application's saved data, NSUnderlyingError=0x7f9c11e4e7a0 "The operation couldn’t be completed. (Cocoa error 134100.)"}), Optional([NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSLocalizedDescription: Failed to initialize the application's saved data, NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x7f9c11e4e760 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes = {
Category = <be1d87d2 e4c72a88 a830fb54 b9aea852 7887b5a6 bf2f85bb 22e878d0 c6916d06>;
ListDetails = <2cc64684 6520b473 d4bc4ae5 39f66fa0 0437788e ef1a0d91 1bdd7b52 86c0fbf2>;
Lists = <c2491da1 58622f56 64112d31 ba3e294a 1167e3f1 5427cd3a a56c1613 e2c0ee8a>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
""
);
NSStoreType = SQLite;
NSStoreUUID = "F84E0B5C-F83B-48ED-9043-5F9B6752FCA4";
"_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}])
我试过在这个网站上发布的解决方案(比如从模拟器中删除应用程序,清理和构建,重置&#34;内容和设置&#34;在模拟器中)但是没有一个在我的情况下工作。
提前致谢
答案 0 :(得分:0)
在iOS编程中,每个程序都安装在沙盒中,这意味着它们是独立的。应用无法访问其他应用中的数据,除非其他应用允许此类操作。
在您的情况下,有一种更简单的预加载数据的方法。您可以将数据存储在用户默认值中。在应用程序启动时,您将检查用户默认值中的数据是否为零。如果是,请以用户默认值( preload )加载数据。然后,将来由于用户默认存在数据,请不要再次加载。