NSMergeConflict on iOS7

后端 未结 6 1939
时光取名叫无心
时光取名叫无心 2021-02-06 01:29

I have updated my app to support iOS 7 and have been faced with the problem that on one of screens in my [context save]; I get the following error:

         


        
相关标签:
6条回答
  • 2021-02-06 01:44

    I got similar error, and in my case, locking NSPersistentStoreCoordinator worked.

    [context.persistentStoreCoordinator lock];
    [context performBlockAndWait:^{
       // do something
    }];
    [context.persistentStoreCoordinator unlock]
    

    I don't know why it works, but I suspect NSManagedObjectContext's bug. I hope this will help.

    0 讨论(0)
  • 2021-02-06 01:58

    I didn't want to mask a potential problem by setting a merge policy without understanding what was causing the NSMergeConflict first.

    In my situation I had performed a NSBatchDeleteRequest earlier on in my code. The NSBatchDeleteRequest is performed directly on the persistent store coordinator so the ManagedObjectContext was unaware of the deletions and still held references to deleted objects. When I later referenced one of these objects and tried to save the context, the NSMergeConflict was thrown.

    Calling reset() on my moc after performing the deletion fixed the issue.

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Tasks")
    let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
    try managedContext.execute(batchDeleteRequest)
    managedContext.reset() 
    
    0 讨论(0)
  • 2021-02-06 02:01

    I just spent two days debugging the exact same error. The difference between your app and mine is that mine only accesses core data from the main thread, so the merge error was even more puzzling.

    In our case I narrowed it down to the fact that we had a unidirectional relationship - A has many Bs (modeled as an NSSet), but B doesn't know its A. We had a method that modified both an A and a B, and would cause a merge error when we went to save those changes. This code worked fine for a long time on both iOS 5 & 6, and only started to fail on iOS 7.

    It's true that adding a merge policy will make the error go away, but it might also mask other errors. In our case we'd rather see those errors than risk having an inconsistent DB.

    Changing the relationship to bidirectional made the error go away. The back links aren't necessary for our app, but they aren't hurting either. (And happily, changing this relationship was handled correctly as a lightweight migration - core data automatically filled in those back links for us.)

    0 讨论(0)
  • 2021-02-06 02:01

    Using Xcode 6.3.2 with Apple Watchkit extension and I got this same error when trying to make multiple updates and saves. The setMergePolicy solved the problem and here are the swift code:

    context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    

    Make sure to put the above line before the context.save command.

    0 讨论(0)
  • 2021-02-06 02:03

    According to apple's documentation

    NSManagedObjectMergeError = 133020

    This Error code to denote that a merge policy failed—Core Data is unable to complete merging.

    Is there any merge policy in your code? Please try NSMergeByPropertyObjectTrumpMergePolicy.

    [self.context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
    
    0 讨论(0)
  • 2021-02-06 02:06

    I got this when testing with a full storage. So it appears that any sort of failure to merge (in my case, the storage is full and the persistent store cannot be updated) will generate this.

    0 讨论(0)
提交回复
热议问题