Database Reference
In-Depth Information
We start this method of by checking to see whether
iCloudIsReady
and to be sure we are awaiting a
move of our local data to iCloud. This may seem a little strange because we are in this method, but
we are making sure that iCloud is ready. If we are truly ready to move our local data to iCloud, we
move on. If not, we set out
_awaitingMoveLocalToiCloud
back to
YES
.
We start by getting an array of
NSURL
s for the documents that reside in our applications documents
directory. Next we enumerate that array. For each
NSURL
in that array we check to be sure the
path extension matches our ict document extension. If it does get the destination URL by calling
getDocumentURL: and passing it the value returned from
getDocumentFiles:forLocal:. then
we
pass our filename without the extension and the final parameter of
NO
. We will modify that method
shortly to handle iCloud. But it is safe to assume at this point that we get back an iCloud URL.
Now we move to a background thread by calling dispatch_async. Using
NSFileManager
we call the
method
setUbiquitous:itemAtURL:destinationURL:error:
, which will move our file from the local
directory and put it in the iCloud container. We check to see whether we were successful and if so
we call
loadDocumentAtFileURL:
passing it our destinationURL. Remember that this method will end
up creating our
CTEntry
object or updating it if necessary. If we weren't successful, we log out the
error so you can see what might have happened. Now let's write the method to copy our documents
from iCloud to our local directory.
-(void)copyiCloudToLocal {
if(_iCloudIsReady && _awaitingCopyiCloudToLocal){
_awaitingCopyiCloudToLocal = NO;
[_iCloudURLs enumerateObjectsUsingBlock:^(NSURL *fileURL, NSUInteger idx, BOOL *stop) {
NSString *fileName = [[fileURL lastPathComponent] stringByDeletingPathExtension];
NSURL *destinationURL = [self getDocumentURL:[self getDocumentFilename:fileName
forLocal:YES]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc]
initWithFilePresenter:nil];
[fileCoordinator coordinateReadingItemAtURL:fileURL
options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL *newURL) {
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
BOOL success = [fileManager copyItemAtURL:fileURL toURL:destinationURL
error:&error];
if(success){
NSLog(@"Copied %@ to %@",fileURL,destinationURL);
[self loadDocumentAtFileURL:destinationURL];
} else {
NSLog(@"Error Copying %@ to %@ - %@",fileURL,destinationURL,
error.localizedDescription);
}
}];
});
}];
Search WWH ::
Custom Search