Database Reference
In-Depth Information
We then set our iCloudIsReady property to YES. This value is going to be used in the methods we
will write to move data to iCloud or to copy data from iCloud.
Next we check to see if iCloudOn returns true. If it is, then we first want to clean up our entries array
by checking if any of our entry fileURLs match an object in our iCloudURLs. If it does, we remove
that entry by calling removeEntryWithURL: and pass the fileURL. This is done to make sure we don't
end up with duplicate entries because next we will enumerate over our iCloudURLs array and call
loadDocumentAtFileURL: and pass the fileURL. If you follow this method you will see that a CTEntry
object will get created and added to our entries array. The last step is to re enable our add button.
If you recall we disabled this when we first called our reload method.
We then want to enable updates by calling the enableUpdates method on our NSMetadataQuery
object. The final thing we do here is check to see if we are awaiting a move of our local data to
iCloud or if we are awaiting copying our iCloud document to our local directory. If we are wanting to
move our local data to iCloud we set our bool to NO . Each of our TODOs will be the actual method
calls that we will write now.
The first method is going to copy our local files to iCloud and should look like this:
-(void)moveLocalToiCloud {
if(_iCloudIsReady && !_awaitingMoveLocalToiCloud){
NSArray *localDocuments = [[NSFileManager defaultManager]
contentsOfDirectoryAtURL:[AppDelegate applicationDocumentsDirectory] includingPropertiesForKeys:nil
options:0 error:nil];
[localDocuments enumerateObjectsUsingBlock:^(NSURL *fileURL, NSUInteger idx, BOOL *stop) {
if([[fileURL pathExtension] isEqualToString:CT_EXTENSION]){
NSString *fileName = [[fileURL lastPathComponent] stringByDeletingPathExtension];
NSURL *destinationURL = [self getDocumentURL:[self getDocumentFilename:fileName
forLocal:NO]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] setUbiquitous:[self iCloudOn]
itemAtURL:fileURL destinationURL:destinationURL error:&error];
if(success){
NSLog(@"Moved %@ to %@",fileURL,destinationURL);
[self loadDocumentAtFileURL:destinationURL];
} else {
NSLog(@"Error Moving %@ to %@ - %@",fileURL,destinationURL,error.
localizedDescription);
}
});
}
}];
} else {
_awaitingMoveLocalToiCloud = YES;
}
}
 
Search WWH ::




Custom Search