I am using what seems like a simple call to a class method NSFileVersion removeOtherVersionsOfItemAtURL:inside a coordinated recording block for some resolution of the iCloud conflict.
When my devices go into "spaz" mode, which is a technical term for repeatedly opening and closing an application on several devices, an exception is EXC_BAD_ACCESSthrown from the inside. Code snippet:
- (void)compareVersionChanges:(NSFileVersion *)version {
if (![DataLoader iCloudPreferenceEnabled]) {
NSLog(@"Ignoring iCloud changes (version comparison) based on user preference");
return;
}
NSLog(@"compareVersionChanges");
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(aQueue, ^(void) {
NSError *readError = nil;
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:(id)self];
[coordinator coordinateReadingItemAtURL:[version URL] options:0 error:&readError byAccessor:^(NSURL *newURL) {
DataContext *loadedContext = nil;
NSData *data = [NSData dataWithContentsOfURL:newURL];
NSError *e = nil;
loadedContext = [self convertXmlDataToContext:data error:&e];
if (e) {
NSLog(@"Done loading, error: %@", e);
[[DataLoader applicationDelegate] displayError:e];
loadedContext = nil;
}
if (!loadedContext) {
return;
}
id appDelegate = [DataLoader applicationDelegate];
DataContext *inMemoryContext = nil;
if (appDelegate != nil && [appDelegate respondsToSelector:@selector(context)]) {
inMemoryContext = [appDelegate performSelector:@selector(context)];
}
if (inMemoryContext) {
NSLog(@"Performing iCloud context synchronizating...");
DataContextSynchronizer *synchronizer = [[DataContextSynchronizer alloc] init];
ChangeSet *changes = [synchronizer compareLocalContext:inMemoryContext andRemoteContext:loadedContext];
if ([[changes changes] count] > 0) {
[SelectionManager disable];
@synchronized(appDelegate) {
NSLog(@"Applying synchronization changes...");
[synchronizer applyChangeSet:changes toDataContext:inMemoryContext];
NSLog(@"Synchronization changes applied");
}
[SelectionManager enable];
if ([appDelegate respondsToSelector:@selector(setSkipRefreshSave:)]) {
[appDelegate performSelector:@selector(setSkipRefreshSave:) withObject:[NSNumber numberWithBool:YES]];
}
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^(void) {
[SelectionManager notifyListeners];
});
if ([appDelegate respondsToSelector:@selector(setSkipRefreshSave:)]) {
[appDelegate performSelector:@selector(setSkipRefreshSave:) withObject:[NSNumber numberWithBool:NO]];
}
[self save:[[DataLoader applicationDelegate] context]];
} else {
NSLog(@"No sync changes applicable.");
}
NSError *coordinateWriteRemoveError = nil;
[coordinator coordinateWritingItemAtURL:newURL options:NSFileCoordinatorWritingForDeleting error:&coordinateWriteRemoveError byAccessor:^(NSURL *theURL) {
theURL = [theURL copy];
NSError *removeOtherVersionsError = nil;
[NSFileVersion removeOtherVersionsOfItemAtURL:theURL error:&removeOtherVersionsError];
if (removeOtherVersionsError) {
NSLog(@"Error removing other versions: %@", removeOtherVersionsError);
}
}];
if (coordinateWriteRemoveError) {
NSLog(@"Error occurred coordinating write for deletion of other file versions: %@", coordinateWriteRemoveError);
}
}
}];
if (readError) {
NSLog(@"Done loading (outside block) error: %@", readError);
}
});
}
I thought a little syntax highlighting could make this easier:
Link to image of code snippet and error stack in Xcode
1404, , , Apple.
, , , -, ? [... copy] 1402 , , , , .
: ! ARC.
2: , :
[NSFileVersion otherVersionsOfItemAtURL:theURL]
nil, ( ):
... nil, . , currentVersionOfItemAtURL:
, , removeOtherVersionsOfItemAtURL:, . , EXC_BAD_ACCESS, , .