开发者

iOS: Core data error: NSMergeConflict for NSManagedObject

I have a core data many to many relationship of article to category. When attempting to save, I get the following error. I am unable to find information about what it means, and why there are two versions when the database is empty. Can anyone shed some light?

Error: NSMergeConflict (0x76ae720) for NSManagedObject (0xd945560) with objectID '0xd943550 <x-coredata://09A438A8-E3F5-45FE-B9D7-106798E82E18/Article/p91>' with oldVersion = 1 and newVersion = 2

Code:

NSMutableDictionary *dict   = [[data objectAtIndex:i] valueForKey:@"category_names"];
        NSMutableArray      *values = [[NSMutableArray alloc] init];
        for (NSString *value in [dict allValues]) {
            NSLog(@"value = %@", value);
            [values addObject:value];
        }

        NSMutableSet *setElements = [[NSMutableSet alloc] init];
        for (int i = 0; i < [values count]; i++) {
            Category *cat = [self getCategoryFor:[values objectAtIndex:i]]; // Function which has fetch to get the category for the value "name"
            [setElements addObject:cat];
        }

        if ([setElements count] > 0) 
            [article addCategories:setElements];


        // Save the context.
        NSError* error;
        if (![managedObjectContext save:&error]) {
            NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
            NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
            if(detailedErrors != nil && [detailedErrors count] > 0) {
                for(NSError* detailedError in detailedErrors) {
                    NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                }
            } else
         开发者_运维问答       NSLog(@"  %@", [error userInfo]);
        }

        [article release];
        [values release];
        [setElements release];


The error your getting is not actually related to the data itself but rather caused by having two irreconcilable versions of the data model writing to the same persistent store file.

You must have created a data model, used it write some data to the persistent store and then updated the model. This is normally not an issue unless you so alter the data model that the automatic migration cannot merge the old and new data.

If this is still under development and you don't need the existing data, the easiest solution is to delete the app off the simulator and then build and run it again using only the latest data model. That will require no migration and will therefore skip the merge error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜