我正在开发我的第一个iCloud App。工作一段时间后,该应用由于“ UIDocumentStateSavingError”而无法再访问UIManagedDocument。有什么方法可以真正找出发生了什么错误?
这是我创建UIManagedDocument的代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
iCloudURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (iCloudURL == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self iCloudNotAvailable];
});
return;
}
iCloudDocumentsURL = [iCloudURL URLByAppendingPathComponent:@"Documents"];
iCloudCoreDataLogFilesURL = [iCloudURL URLByAppendingPathComponent:@"TransactionLogs"];
NSURL *url = [iCloudDocumentsURL URLByAppendingPathComponent:@"CloudDatabase"];
iCloudDatabaseDocument = [[UIManagedDocument alloc] initWithFileURL:url];
NSMutableDictionary *options = [NSMutableDictionary dictionary];
NSString *name = [iCloudDatabaseDocument.fileURL lastPathComponent];
[options setObject:name forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:iCloudCoreDataLogFilesURL forKey:NSPersistentStoreUbiquitousContentURLKey];
iCloudDatabaseDocument.persistentStoreOptions = options;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentContentsChanged:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:iCloudDatabaseDocument.managedObjectContext.persistentStoreCoordinator];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentStateChanged:) name:UIDocumentStateChangedNotification object:iCloudDatabaseDocument];
if ([[NSFileManager defaultManager] fileExistsAtPath:[iCloudDatabaseDocument.fileURL path]]) {
// This is true, the document exists.
if (iCloudDatabaseDocument.documentState == UIDocumentStateClosed) {
[iCloudDatabaseDocument openWithCompletionHandler:^(BOOL success) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self documentConnectionIsReady];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self connectionError:iCloudConnectionErrorFailedToOpen];
});
}
}];
} else if (iCloudDatabaseDocument.documentState == UIDocumentStateNormal) {
...
}
} else {
...
}
});
该文档已经存在,因此在文档上调用了openWithCompletionHandler:。这将失败,并触发UIDocumentStateChangedNotification,显示文档状态为5:
UIDocumentStateClosed和
UIDocumentStateSavingError
此后,完成块将被调用。从这里开始的正确方法是什么?有什么方法可以找出问题所在和发生了哪种错误?
我试图在完成块中重新打开文档,但结果是相同的。
我想我可以通过删除文件并重新创建来解决问题。但这显然不是一旦应用程序将在商店中推出后的选择。我想知道出了什么问题,并为用户提供一种适当的方式来解决该问题。
我已经在这里检查了处理UIDocumentStateSavingError的其他问题(它们很多),但是似乎不适用于这里的问题。
知道如何找出问题所在吗?我不敢相信API会告诉您“保存期间出了点问题,但我不会告诉您!”
最佳答案
您可以在完成处理程序中查询documentState。不幸的是,如果您想要确切的错误,则获取该错误的唯一方法是子类化并覆盖handleError:userInteractionPermitted:
也许这样的事情会有所帮助(无需编译器而徒手输入)...
@interface MyManagedDocument : UIManagedDocument
- (void)handleError:(NSError *)error
userInteractionPermitted:(BOOL)userInteractionPermitted;
@property (nonatomic, strong) NSError *lastError;
@end
@implementation MyManagedDocument
@synthesize lastError = _lastError;
- (void)handleError:(NSError *)error
userInteractionPermitted:(BOOL)userInteractionPermitted
{
self.lastError = error;
[super handleError:error
userInteractionPermitted:userInteractionPermitted];
}
@end
然后,您可以像这样创建它...
iCloudDatabaseDocument = [[UIManagedDocument alloc] initWithFileURL:url];
并像这样在完成处理程序中使用它...
[iCloudDatabaseDocument openWithCompletionHandler:^(BOOL success) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self documentConnectionIsReady];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self connectionError:iCloudConnectionErrorFailedToOpen
withError:iCloudDatabaseDocument.lastError];
});
}
}];