Please let questions about "The CloudKit framework introduces support for long-lived operations" is described in the "What's New in iOS 9.3".
(https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS9_3.html#//apple_ref/doc/uid/TP40016661-SW1)
I did the following operations.
1. I uploaded a dummy file of approximately 100MB to "PUBLIC DATABASE" > "Default Zone" in the "CloudKit Dashboard".
2. I created the iOS app for the test, such as the following.
2-1. Setting CKOperation.longLived to YES.
2-2. Gettng the uploaded dummy files using the CKFetchRecordsOperation object.
2-3. In order to confirm that the CompletionBlock of CKFetchRecordsOperation has been called, saving the log to UserDefaults.
3. I using the iOS app ("2"), it has got the file by CKFetchRecordsOperation object.
4. Before the getting of the file is completed, I exit the app by swiping up in the "App Switcher".
After you exit the app, I confirmed that the application capacity is increasing.
(I saw "Settings" > "General" > "Storage & Cloud Usage" > "STORAGE" > "Manage Storage")
Therefore, I think the file was got by "long-lived operations".
However, CompletionBlock does not seem to be called then. Because nothing has been saved in UserDefaults.
How should I do it to handle the data which I got when application is not running?
The following is the code of the test application.
----------
- (IBAction)test:(id)sender {
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:OSSRecordId];
NSArray *fetchRecordIDs = [[NSArray alloc] initWithObjects:recordID, nil];
CKFetchRecordsOperation *fetchRecordsOperation = [[CKFetchRecordsOperation alloc] initWithRecordIDs:fetchRecordIDs];
// Record fetch completed
fetchRecordsOperation.perRecordCompletionBlock = ^(CKRecord *record,
CKRecordID *recordID, NSError *error) {
// User Defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *defaultsValue = [defaults objectForKey:OSSUserDefaultsKeyRecordID];
NSString *string = [NSString stringWithFormat:@"%@/%@", defaultsValue, recordID.recordName];
[defaults setObject:string forKey:OSSUserDefaultsKeyRecordID];
};
// All fetch completed
fetchRecordsOperation.fetchRecordsCompletionBlock = ^(NSDictionary
*recordsByRecordID, NSError *error) {
// User Defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *defaultsValue = [defaults objectForKey:OSSUserDefaultsKeyRecordID];
NSString *string = [NSString stringWithFormat:@"%@/%s", defaultsValue, "fetchRecordsCompletionBlock"];
[defaults setObject:string forKey:OSSUserDefaultsKeyRecordID];
};
// Long-Lived
fetchRecordsOperation.longLived = YES;
fetchRecordsOperation.longLivedOperationWasPersistedBlock = ^() {
// User Defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *defaultsValue = [defaults objectForKey:OSSUserDefaultsKeyRecordID];
NSString *string = [NSString stringWithFormat:@"%@/%s", defaultsValue, "longLivedOperationWasPersistedBlock"];
[defaults setObject:string forKey:OSSUserDefaultsKeyRecordID];
};
fetchRecordsOperation.database = [[CKContainer defaultContainer] publicCloudDatabase];
[fetchRecordsOperation start];
}
----------