Search code examples
iosparse-platformnsfilemanagerparse-server

What is the NSFileProtection level for PFFile.getData() on Parse iOS SDK?


An app seems to crash very rarely, an Apple Engineer suggested that the cause might be that the crash happens if the user locks their device between the time when the NSData object is created and when the application attempts to read the first byte from that NSData object.

This would mean that Parse is downloading/storing a file with the 'Complete' data protection level (NSFileProtectionComplete).

let query = PFQuery(className: aClass)
let result = try query.getFirstObject()

guard
    let imageObject = result.objectForKey(aKey) as? PFFile,
    let imageData = try? imageObject.getData(),
    let firstByte = imageData.dataType() // this reads the 1st byte of data
else {
    return
}

The Parse iOS SDK seems to use NSFileProtectionCompleteUntilFirstUserAuthentication, which allows file access after the first user authentication/unlock after the device was booted.

static NSDictionary *_PFFileManagerDefaultDirectoryFileAttributes() {
#if !PF_TARGET_OS_OSX
    return @{ NSFileProtectionKey : NSFileProtectionCompleteUntilFirstUserAuthentication };
#else
    return nil;
#endif
}

static NSDataWritingOptions _PFFileManagerDefaultDataWritingOptions() {
    NSDataWritingOptions options = NSDataWritingAtomic;
#if !PF_TARGET_OS_OSX
    options |= NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication;
#endif
    return options;
}

@interface PFFileManager ()
@property (nonatomic, copy) NSString *applicationIdentifier;
@property (nonatomic, copy) NSString *applicationGroupIdentifier;
@end
@implementation PFFileManager

So am I right in assuming that the synchronous PFFile.getData() uses NSFileProtectionCompleteUntilFirstUserAuthentication? Or does it not use a file protection at all because the data is loaded into memory and not written to a file at all?


Solution

  • After further digging into the Parse iOS SDK I can answer the question:

    Synchronous (like asynchronous) file fetching saves the data in a temporary file with NSFileManager and NSFileProtectionCompleteUntilFirstUserAuthentication.