UIManagedDocument saveToURL completionHandler is not called - Error message: “The reader is not permitted to access the URL.”

℡╲_俬逩灬. 提交于 2019-12-06 01:23:54

问题


I've got an old app that uses UIManagedDocument to interact with Core Data. However on iOS 11.2 (and possibly earlier iOS 11 point releases) the saveToURL:forSaveOperation:completionHandler: method seems to have stopped working both on-device and in the simulator (however it does still work in the iOS 10.3.1 simulator). Specifically, in the code below the completionHandler inside the first if statement is never executed (as NSLog messages indicate).

- (void)useDemoDocument {
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"TWL_Document"];
    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];

    if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        NSLog(@"This Code Executes");
        [document saveToURL:url
           forSaveOperation:UIDocumentSaveForCreating
          completionHandler:^(BOOL success) {
              if (success) {
                  NSLog(@"But this is never called");
                  self.managedObjectContext = document.managedObjectContext;
              } else {
                  NSLog(@"This also is not called");
              }
          }];
    } else if (document.documentState == UIDocumentStateClosed) {
        [document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = document.managedObjectContext;
            }
        }];
    } else {
        self.managedObjectContext = document.managedObjectContext;
    }
}

Instead I get an error message that The reader is not permitted to access the URL.:

2017-12-17 12:38:14.258936-0800 ToWatchList[1864:542434] [default] [ERROR] Could not get attribute values for item /var/mobile/Containers/Data/Application/2[UUID]/Documents/TWL_Document (n). Error: Error Domain=NSFileProviderInternalErrorDomain Code=1 "The reader is not permitted to access the URL." UserInfo={NSLocalizedDescription=The reader is not permitted to access the URL.}

What's going on here? Any suggestions on how to get this working again in iOS 11?

来源:https://stackoverflow.com/questions/47859376/uimanageddocument-savetourl-completionhandler-is-not-called-error-message-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!