我正在開發我的第一個iCloud應用程序。在工作了一段時間之後,由於「UIDocumentStateSavingError」,應用程序無法再訪問UIManagedDocument。有什麼方法可以真正找出發生了什麼錯誤?UIManagedDocument - 如何處理UIDocumentStateSavingError?
這是我的代碼來創建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告訴你「保存期間出現問題,但我不會告訴你什麼!」