Re: CoreData "Could Not Merge Changes"
Re: CoreData "Could Not Merge Changes"
- Subject: Re: CoreData "Could Not Merge Changes"
- From: mmalcolm crawford <email@hidden>
- Date: Sun, 14 Aug 2005 09:32:53 -0700
On Aug 14, 2005, at 6:00 AM, Jesse Grosjean wrote:
So now I'm not sure how to get around this problem.
I'm not sure either, I'm afraid, but it probably does have something
to do with:
it looks odd that both the new and old version have the same
version number.
What's curious is that it does not occur if you go through the
sequence "manually" (IIUC):
NSError *error = nil;
BOOL ok;
NSManagedObjectContext *moc = [self managedObjectContext];
ok = [moc save:&error];
if (!ok) {
NSLog(@"Error in save:\n%@", [error localizedDescription]);
return;
}
NSManagedObject *mo = [NSEntityDescription
insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:moc];
[mo setValue:@"new name" forKey:@"name"];
ok = [moc save:&error];
if (!ok) {
NSLog(@"Error in save:\n%@", [error localizedDescription]);
return;
}
else {
NSLog(@"Committed values for instance:\n%@", [mo
committedValuesForKeys:nil]);
}
[moc undo];
[moc undo];
ok = [moc save:&error];
if (!ok) {
NSLog(@"Error in save:\n%@", [error localizedDescription]);
return;
}
else {
NSLog(@"Two undos");
// mo correctly inaccessible as fault here
}
[moc redo];
ok = [moc save:&error];
if (!ok) {
NSLog(@"Error in save:\n%@", [error localizedDescription]);
return;
}
else {
NSLog(@"Redid OK");
}
Making several assumptions, you can sort of work around it using -
rollback, but I haven't worked through this properly yet (notably the
final segment of the attemptRecoveryFromError method) -- and I really
must get to sleep at some point...
mmalc
// omitted proper localisation
- (NSError *)willPresentError:(NSError *)error
{
if ([error code] != NSManagedObjectMergeError) {
return error;
}
NSArray *conflictList = [[error userInfo]
objectForKey:@"conflictList"];
if ([conflictList count] != 1) {
return error;
}
NSManagedObject *mergeObject = [[conflictList objectAtIndex:0]
objectForKey:@"object"];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:error forKey:NSUnderlyingErrorKey];
NSString *localizedDescription = [NSString stringWithFormat:
@"Could not merge changes for \"%@\"", [mergeObject
valueForKey:@"name"]];
[userInfo setObject:localizedDescription
forKey:NSLocalizedDescriptionKey];
NSString *localizedRecovery = @"Overwrite changes?";
[userInfo setObject:localizedRecovery
forKey:NSLocalizedRecoverySuggestionErrorKey];
NSArray *localizedRecoveryOptions = [NSArray
arrayWithObjects:@"Yes", @"No", nil];
[userInfo setObject:localizedRecoveryOptions
forKey:NSLocalizedRecoveryOptionsErrorKey];
[userInfo setObject:self forKey:NSRecoveryAttempterErrorKey];
NSError *newError = [NSError errorWithDomain:@"MyDomain"
code:911 userInfo:userInfo];
return newError;
}
- (void)attemptRecoveryFromError:(NSError *)error
optionIndex:(unsigned int)recoveryOptionIndex
delegate:(id)delegate
didRecoverSelector:(SEL)didRecoverSelector
contextInfo:(void *)contextInfo
{
BOOL success = NO;
if (recoveryOptionIndex == 0) {
NSError *originalError = [[error userInfo]
objectForKey:NSUnderlyingErrorKey];
NSArray *conflictList = [[originalError userInfo]
objectForKey:@"conflictList"];
if ([conflictList count] == 1) {
NSManagedObject *mergeObject = [[conflictList objectAtIndex:0]
objectForKey:@"object"];
[[mergeObject managedObjectContext] rollback];
success = YES; // ??
}
}
// ... then this should be ... ?
NSInvocation *invoke = [NSInvocation invocationWithMethodSignature:
[delegate methodSignatureForSelector:didRecoverSelector]];
[invoke setSelector:didRecoverSelector];
[invoke setArgument:(void *)&success atIndex:2];
[invoke setArgument:contextInfo atIndex:3];
[invoke invokeWithTarget:delegate];
// ... but it generates a SIGBUS 10 ...
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden