我正在使用似乎是在协调写入块中对NSFileVersion
类方法的简单调用,以解决某些 iCloud 冲突。removeOtherVersionsOfItemAtURL:
当我的设备进入“spaz 模式”时,这是一个技术术语,用于在一些设备上重复打开和关闭应用程序,EXC_BAD_ACCESS
内部会引发异常。代码片段:
- (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);
}
});
}
我认为一点语法突出显示可能会使这更容易检查:
该错误实际上发生在第 1404 行,从下面的屏幕截图中可以看出,它在 Apple 代码领域很深。
在提交雷达之前,我想我会在这里检查一下我是否做错了什么?第 1402 行的额外[... copy]
内容只是一个快速检查,以确保我不会丢失对块提供参数的引用,并将被删除。
编辑:重要提示!我正在使用ARC。
编辑2:我注意到打电话时:
[NSFileVersion otherVersionsOfItemAtURL:theURL]
返回值为nil
,这表明(通过文档):
...如果没有这样的文件,则为 nil。该数组不包含 currentVersionOfItemAtURL: 方法返回的版本对象。
因此,通过在调用之前检查此方法的返回值removeOtherVersionsOfItemAtURL:
,它已经缓解了这个问题。但是我仍然觉得奇怪的是抛出了 EXC_BAD_ACCESS,而不是正确处理它的方法。