【问题标题】:Preloading data into core data in iOS 8 app在 iOS 8 应用程序中将数据预加载到核心数据中
【发布时间】:2015-04-21 16:15:21
【问题描述】:

我正在制作一个 swift 应用程序,我需要在其中预加载核心数据。为此,我创建了另一个示例 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}])

我尝试了在此站点上发布的解决方案(例如从模拟器中删除应用程序、清理和构建、在模拟器中重置“内容和设置”),但在我的情况下没有一个有效。

提前致谢

【问题讨论】:

    标签: ios sqlite swift core-data


    【解决方案1】:

    在 iOS 编程中,每个程序都安装在一个沙盒中,这意味着它们是独立的。一个应用不能访问另一个应用的数据,除非另一个应用允许这样的操作。

    在您的情况下,有一种更简单的方法来预加载数据。您可以将数据存储在用户默认值中。在您的应用程序启动时,您检查用户默认值中的数据是否为零。如果是这样,在用户默认(preload)中加载数据。那么以后由于用户默认有数据,就不要再加载了。

    【讨论】:

      猜你喜欢
      • 2012-01-30
      • 2014-04-16
      • 2011-06-11
      • 2011-07-06
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多