NSMergeConflict issue when commit thread?
I am doing someFunction that will Donwload data from MyAPI and save to core data repeatly
example
======================
=========================
may be the time like that.. When the function is not finished yet, I call that met开发者_C百科hod again and again at different thread
when I am commit that thread with this function
+(void)commit {
// get the moc for this thread
NSManagedObjectContext *moc = [self managedObjectContext];
NSThread *thread = [NSThread currentThread];
DLog(@"threadKey commit%@" , [[self class]threadKey]);
if ([thread isMainThread] == NO) {
// only observe notifications other than the main thread
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:moc];
}
NSError *error;
if (![moc save:&error]) {
DLog(@"Error in Saving %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
//NSString * temp= [NSString stringWithFormat: @"There is error In Saving.. Contact us! :P",error];
//[[BNUtilitiesQuick UtilitiesQuick] Alert:temp];
});
}
if ([thread isMainThread] == NO) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:moc];
}
}
(this code to save all data to cache..)
there is error, the error like that
"NSMergeConflict (0x1205b7f0) for NSManagedObject (0x10900d70) with objectID
'0x10cad920 <x-coredata://60AE38D6-EE4C-4FE1-9B9A-5322E301E7D5/City/p2>' with oldVersion = 20 and newVersion = 21 and old object snapshot =
{\n Country = \"0x10611dd0 <x-coredata://60AE38D6-EE4C-4FE1-9B9A-5322E301E7D5/Country/p1>\";\n Name = \"Jakarta Barat\";\n} and new cached row =
{\n Country = \"0x1019e1f0 <x-coredata://60AE38D6-EE4C-4FE1-9B9A-5322E301E7D5/Country/p1>\";\n Name = \"Jakarta Barat\";\n}"
any one can tell me what error is it?
For example, what is 0x10611dd0? Is that memory location?
What is is that core data id?
How the error shows up anyway? Looks like coredata try to merge changes that's already the same.
Yes several thread manage DIFFERENT managedobjectcontext at the same time. All those coredata has the same persistent store.
The problem here is that you try to merge two object with the same objectID but different values for Country. The hex after country is the address for this property, so it seems that you have two different objects set for the country and that causes the merge error.
精彩评论