iOS 9 CoreData / ICloud - No such document at URL

不羁岁月 提交于 2019-12-07 23:51:47

问题


UPDATE 2

I am also occasionally getting this error:

CoreData: Ubiquity:  Librarian returned a serious error for starting downloads Error Domain=BRCloudDocsErrorDomain Code=6

I am wondering if it is related? Am working on submitting a bug report but would appreciate any insight.

UPDATE

When this error occurs I am getting very strange behavior with coredata where it will not be able to find related objects in the same context. This is absolutely crippling my app now

ORIGINAL QUESTION

I have an app that seems to work perfectly 90% of the time syncing CoreData with iCloud Ubiquitous storage.

Sometimes I receive this error and things start going a little crazy:

CoreData: Ubiquity:  Librarian returned a serious error for starting downloads Error Domain=BRCloudDocsErrorDomain Code=5 "No document at URL"

I have searched to find information about how to fix this, but I am not seeing anything to help me in the other questions that have been posted. Many people simply stating that they just gave up trying to fix it.

Can anyone see any issues with my core data stack that would cause this?! I feel like im taking crazy pills.

// MARK: - Core Data stack

lazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = NSBundle.mainBundle().URLForResource("Model", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
    }()

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 documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as NSURL!

    let storeURL = documentsDirectory.URLByAppendingPathComponent("ArrivedAlive.sqlite")

    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    let storeOptions = [NSPersistentStoreUbiquitousContentNameKey: "ArrivedAliveStore", NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]

    do {
        try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: storeOptions)
    } 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"
        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()
    } catch {
        fatalError()
    }

    return coordinator
    }()


lazy var managedObjectContext: NSManagedObjectContext? = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }
    var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)

    managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    managedObjectContext.persistentStoreCoordinator = coordinator

    return managedObjectContext
    }()

回答1:


For those of you out there struggling with these issues - let me give you some good news:

To solve the problem follow these steps:

  1. Download and implement the Ensembles GitHub
  2. Add very small amount of code to your appDelegate to create and manage the ensemble object
  3. Cry out your pent up frustration - you are done.

IT FIXED ALL CLOUD SYNCING ERRORS

It just works like magic and I couldn't be happier. It essentially works like a middle man between core data and iCloud to make sure nobody has a seizure mid thought.



来源:https://stackoverflow.com/questions/33040532/ios-9-coredata-icloud-no-such-document-at-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!