iCloud: Callback for NSFileManager's startDownloadingUbiquitousItemAtURL?
Asked Answered
K

2

19

I am using NSFileManager's startDownloadingUbiquitousItemAtURL to download file from iCloud to local copy (the local does not yet have the copy of the file in this case). I can't seem to find the callback for this process. I need the callback to tell my app that the requested file is finished downloaded (to local copy) so other task can start reading the content of the file and does stuff.

I can check to see if the file is downloaded. But it would involve in polling constantly. Is there a way to do this without setting a timer to poll for this?

Kerley answered 12/3, 2012 at 21:49 Comment(0)
L
33

I believe that this method is intended to be used in conjunction with file coordinators based on Apple's documentation. So you would need to use a file coordinator like so:

NSURL *itemURL = nil; // this is the URL you want to read from

__block NSData *data = nil;
NSError *error = nil;
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[coordinator coordinateReadingItemAtURL:itemURL options:0 error:&error byAccessor:^(NSURL *newURL) {
    data = [NSData dataWithContentsOfURL:newURL];
}];

This will be synchronous, however, so if you wanted to do something asynchronously, you could use blocks as so:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // read info from the URL using the code above
    dispatch_async(dispatch_get_main_queue(), ^{
        // handle data read from the URL
    });
});
Lathrop answered 14/3, 2012 at 15:30 Comment(6)
The method in NSFileCoordinator works beautifully in the synchronous mode. I also needed the async version in the later stage of my project - up voted for that! Thank you.Kerley
@SpaceDog You are 100% right. I've been dealing with these APIs and frankly, the suck.Dear
The APIs created by Apple can be divided in two categories: 1) those which suck, are confused as hell, vague, incomplete and bad documented and 2) those who are passable. There is no single API that is well designed. Don't take me wrong. I love the products Apple do but the Apple that deals with developers & APIs is not the Apple with high standards people know. On the contrary.Cambridge
@SpaceDog I'm printing these comments out right now to hang on my wall. You've summed up my thoughts perfectly.Dear
I usually say that Apple have two CEOs: Tim Cook that runs the Apple we know and satan who runs the Apple that deals with developers. 😃Cambridge
Thanks. It should be mentioned that the file coordinator will trigger the download (so there's no need to call startDownloadingUbiquitousItemAtURL() anymore) and call the block when it has completed, and there will be no progress callback unfortunately.Disharmonious
R
-1

To upload correctly to iCloud you're going to also need something like this (anything else didn't work :| )

   NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

   NSString *FileNS = @"myfile.dat";
   NSURL *FileURL = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:FileNS ];


   BYTE *data = NULL;//Put your file data in here
   int FileSize = 0;//replace with your file size
   NSData *myData = [[NSData alloc] initWithBytes:data length:FileSize];
   [myData writeToFile:[FileURL path] atomically:YES];
Resorcinol answered 2/12, 2012 at 23:30 Comment(1)
op asked for download, not uploadCambridge

© 2022 - 2024 — McMap. All rights reserved.