未调用UIManagedDocument saveToURL completionHandler - 错误消息:"不允许读者访问URL."

Nic*_*ick 7 objective-c ios uimanageddocument ios11

我有一个UIManagedDocument用于与Core Data交互的旧应用程序.然而在iOS 11.2(甚至更早的iOS 11点发布)的saveToURL:forSaveOperation:completionHandler:方法似乎已经停止工作无论在设备和模拟器(但它确实在iOS版10.3.1模拟器仍然有效).具体来说,在下面的代码中,completionHandler内部第一个if语句永远不会执行(如NSLog消息所示).

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

相反,我收到一条错误消息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.}
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?有关如何在iOS 11中重新运行的任何建议吗?