简体   繁体   English

由于核心数据迁移,iOS应用在启动屏幕上崩溃

[英]iOS app crashing on launch screen due to core data migration

This is the code: 这是代码:

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.
    let filemgr = FileManager.default

    let documentDirectoryURL = try! FileManager.default.url(for: .documentDirectory,
                                                            in: .userDomainMask,
                                                            appropriateFor: nil,
                                                            create: true)
    let databaseURL = documentDirectoryURL.appendingPathComponent(“def.sqlite")
    var error1 : NSError?
    let fileExists = (databaseURL as NSURL).checkResourceIsReachableAndReturnError(&error1)
    if(fileExists){
        self.copyFile()
    }
    var myOptions = Dictionary<String, Any>()
    myOptions[NSMigratePersistentStoresAutomaticallyOption] = true
    myOptions[NSInferMappingModelAutomaticallyOption] = true
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let ext = ".sqlite"
    let url = self.applicationDocumentsDirectory.appendingPathComponent((Utils.getPreferenceValue(ForKey: Constants.LOGIN_USER_NAME))+ext)//".sqlite")

    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: myOptions)
    } catch var error1 as NSError {
        error = error1
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
        dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
        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()
    } catch {
        fatalError()
    }

    return coordinator
}

This is the error I am getting. 这是我得到的错误。

returned error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." 返回错误错误域= NSCocoaErrorDomain代码= 134110“持久存储迁移期间发生错误。” UserInfo={sourceURL=file:///var/mobile/Containers/Data/Application/2963C60A-11D3-4A6F-9496-623F5EFCFCB1/Documents/me.gundmi@abc.in.sqlite, reason=Can't copy source store to destination store path, destinationURL=file:///var/mobile/Containers/Data/Application/2963C60A-11D3-4A6F-9496-623F5EFCFCB1/Documents/.me.gundmi@abc.in.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x1c4a40480 {Error Domain=NSCocoaErrorDomain Code=516 "The file “.me.gundmi@abc.in.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3” couldn't be saved in the folder “Documents” because a file with the same name already exists." UserInfo = {sourceURL = file:///var/mobile/Containers/Data/Application/2963C60A-11D3-4A6F-9496-623F5EFCFCB1/Documents/me.gundmi@abc.in.sqlite,原因=无法复制源存储到目标商店路径,destinationURL = file:///var/mobile/Containers/Data/Application/2963C60A-11D3-4A6F-9496-623F5EFCFCB1/Documents/.me.gundmi@abc.in.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3,NSUnderlyingError = 0401 {Error Domain = NSCocoaErrorDomain代码= 516“文件“ .me.gundmi @ abc.in.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3”无法保存在文件夹“ Documents”中,因为已经存在具有相同名称的文件。” UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/2963C60A-11D3-4A6F-9496-623F5EFCFCB1/ 的UserInfo = {NSFilePath =的/ var /移动/容器/数据/应用/ 2963C60A-11D3-4A6F-9496-623F5EFCFCB1 /

如果它们很大,则您可能要考虑控制迁移,并将其放在后台线程中或将其分解成几部分,以便应用程序可以按时启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM