应用程序在后台时写入的文件不会保存

时间:2018-05-08 10:57:26

标签: ios objective-c

我正在开发一个可以通过webRTC在后台接收消息/文件的应用程序。

收到文件后,我将其写入磁盘。但是当稍后尝试访问它时(甚至在应用程序启动之间)该文件不存在。

用户的文档文件夹具有NSFileProtectionCompleteUntilFirstUserAuthentication属性。

我尝试使用NSData的{​​{1}},[writeToURL:options:error:]在磁盘上创建文件; [writeToFile:options:error:]的{​​{1}}以及NSFileManager的方法。

所有这些都在指定的路径/网址上成功创建了文件。在创建之后,我检查文件是否存在[createFileAtPath:contents:attributes:]的{​​{1}},其中显示了以下内容:

NSFileHandle

NSFileManager还向我显示该文件在写入后存在。

考虑到它可能是一个线程问题,我也尝试将该文件写入主线程块,但结果是一样的。第一个文件似乎是写的,但是,当试图访问它后,它就像它从来没有。

有什么我可以遗失的吗?

编辑:添加了我用来写的功能。

[attributesOfItemAtPath:error:]

输出为(文档是应用程序中的用户attributes: { NSFileCreationDate = "2018-05-07 18:47:50 +0000"; NSFileExtensionHidden = 0; NSFileGroupOwnerAccountID = 501; NSFileGroupOwnerAccountName = mobile; NSFileModificationDate = "2018-05-07 18:47:50 +0000"; NSFileOwnerAccountID = 501; NSFileOwnerAccountName = mobile; NSFilePosixPermissions = 420; NSFileProtectionKey = NSFileProtectionCompleteUntilFirstUserAuthentication; NSFileReferenceCount = 1; NSFileSize = 92156; NSFileSystemFileNumber = 695101; NSFileSystemNumber = 16777219; NSFileType = NSFileTypeRegular; }

[[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]

获取文件(图像):

- (void) saveFileData:(NSData *)fileData completionHandler:(void(^)(BOOL success))completionHandler {
   NSURL *fileURL = [self fileURL];

   NSError *error = nil;
   [fileData writeToURL:fileURL options:NSDataWritingAtomic error:&error];

   if (error) {
      ZLogError(ZLogTypeFile, @"[%@] could not be saved: %@", self.fileKey, error);
      completionHandler(NO);
      return;
   }

   ZLogDebug(ZLogTypeFile, @"<file: %@> exists after write:%d", fileURL, [[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]);
   NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:&error];
   ZLogDebug(ZLogTypeFile, @"attributes: %@", attributes);

   completionHandler(YES);
}
记录中的

(注意我刚刚在Documents目录之前取出了长路径):

NSDocumentDirectory

0 个答案:

没有答案