iOS-swift-CoreData 오류 오류: -addPersistentStoreWithType

4937 단어
CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/507F9D1A-FF55-4C23-8750-2289C93580AC/Documents/SingleViewCoreData.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store." UserInfo={metadata={
    NSPersistenceFrameworkVersion = 752;
    NSStoreModelVersionHashes =     {
        LinckoNoteModel = <937f15a4 c4c39faa d974c9ee ced6ebd5 4cad88aa 9b676d8f 6fd13027 01091598>;
        UnUploadNoteModel = <1b336bc4 ad535bb8 50a611a3 95a1b236 16194371 7b381a16 5cc4c1b3 14c2c7cd>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "B9888682-B292-4B29-B13D-70FCE6B8176F";
    "_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 = 752;
        NSStoreModelVersionHashes =         {
            LinckoNoteModel = <937f15a4 c4c39faa d974c9ee ced6ebd5 4cad88aa 9b676d8f 6fd13027 01091598>;
            UnUploadNoteModel = <1b336bc4 ad535bb8 50a611a3 95a1b236 16194371 7b381a16 5cc4c1b3 14c2c7cd>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "B9888682-B292-4B29-B13D-70FCE6B8176F";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
이유: coredata 데이터 마이그레이션이 완료되지 않았습니다.
해결 방법: 앱 대리자에서 지연 로드 재정의
   lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        // The persistent store coordinator for the application. This implementation creates and returns 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
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
        } catch {
            //版本迁移
            var sqlPathRoot:NSString = url.absoluteString
            var shmurl:NSURL = NSURL(string: sqlPathRoot.stringByAppendingString("-shm"))!
            var walurl:NSURL = NSURL(string: sqlPathRoot.stringByAppendingString("-wal"))!
            do{
                try NSFileManager.defaultManager().removeItemAtURL(url)
            }catch{
                print(error)
            }
            do{
                try NSFileManager.defaultManager().removeItemAtURL(shmurl)
            }catch{
                print(error)
            }
            do{
                try NSFileManager.defaultManager().removeItemAtURL(walurl)
            }catch{
                print(error)
            }

            //再创建一次
            var persistentStore : NSPersistentStore?

            do{
                try persistentStore =  coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
            }catch{
                // Report any error we got.
                var dict = [String: AnyObject]()
                dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
                dict[NSLocalizedFailureReasonErrorKey] = failureReason
                dict[NSUnderlyingErrorKey] = error as NSError
                let wrappedError = 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 \(wrappedError), \(wrappedError.userInfo)")
                abort()
            }
            if persistentStore == nil
            {
                print("版本更替失败了")
            }
            else
            {
                print("版本更替成功")
            }

        }
        return coordinator
    }()

좋은 웹페이지 즐겨찾기