我正在开发我的第一个iCloud应用程序。工作一段时间后,由于" 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告诉你"保存期间出了问题,但我不会告诉你什么!"
答案 0 :(得分:5)
您可以在完成处理程序中查询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];
});
}
}];
答案 1 :(得分:1)
基于@JodyHagins优秀代码段,我创建了一个UIDocument子类。
@interface SSDocument : UIDocument
- (void)openWithSuccess:(void (^)())successBlock
failureBlock:(void (^)(NSError *error))failureBlock;
@end
@interface SSDocument ()
@property (nonatomic, strong) NSError *lastError;
@end
@implementation SSDocument
- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
self.lastError = error;
[super handleError:error userInteractionPermitted:userInteractionPermitted];
}
- (void)clearLastError {
self.lastError = nil;
}
- (void)openWithSuccess:(void (^)())successBlock failureBlock:(void (^)(NSError *error))failureBlock {
NSParameterAssert(successBlock);
NSParameterAssert(failureBlock);
[self clearLastError];
[self openWithCompletionHandler:^(BOOL success) {
if (success) {
successBlock();
} else {
NSError *error = self.lastError;
[self clearLastError];
failureBlock(error);
}
}];
}
@end