1

I'm writing a Unity plugin which makes use of iCloud.

Now the way I currently use to move files to iCloud is using this code:

     [byteDocument saveToURL:savePathURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
     {
         if (ubiquitousContainerURL != nil)
         {
             NSURL *ubiquityDocumentDirectory = [ubiquitousContainerURL
                                                 URLByAppendingPathComponent:@"Documents"
                                                 isDirectory:YES];

             ubiquityDocumentDirectory = [ubiquityDocumentDirectory URLByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", filename, extension]];

             NSFileManager* fm = [NSFileManager defaultManager];

             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                 NSError* error = nil;

                 if (![fm fileExistsAtPath:ubiquityDocumentDirectory.path])
                 {
                     NSLog(@"Attemping to move to cloud");
                     didMoveToCloud = [fm setUbiquitous:YES itemAtURL:savePathURL destinationURL:ubiquityDocumentDirectory error:nil];
                 }
                 else
                 {
                     NSLog(@"Attemping to replace in cloud");
                     didMoveToCloud = [fm replaceItemAtURL:ubiquityDocumentDirectory withItemAtURL:savePathURL backupItemName:nil options:0 resultingItemURL:nil error:&error];
                 }

                 if (didMoveToCloud)
                 {
                     NSLog(@"Did move text document successfully to cloud");
                     //UnitySendMessage("GameObject", "DidSaveFile", [savePathURL.path cStringUsingEncoding:NSASCIIStringEncoding]);
                 }
                 else
                 {
                     NSLog(@"Failed to move text document to cloud");
                     //UnitySendMessage("GameObject", "DidSaveFile", [savePathURL.path cStringUsingEncoding:NSASCIIStringEncoding]);
                 }

                 if (error != nil)
                 {
                     NSLog(@"Error saving text document to cloud: %@", error.description);
                     //UnitySendMessage("GameObject", "DidSaveFile", [savePathURL.path cStringUsingEncoding:NSASCIIStringEncoding]);
                 }
             });
         }
     }];

Basically I'm just using a simple inherited class of UIDocument called CloudByteDocument. I first save it locally on the device and then move it to iCloud afterwards.

Now the problem is that using SetUbiquituos does not seem to provide any way of tracking the upload progress? I'm already attached to both MSMetaDataQuery observerser, that is:

 // Listen for the second phase live-updating
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(queryDidReceiveNotification:) name:NSMetadataQueryDidUpdateNotification object:nil];

// Listen for the first phase gathering
[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(queryIsDoneGathering:) name:NSMetadataQueryDidFinishGatheringNotification
                                           object:nil];

And I also realize that you can get things like upload and download progress during the NSMetadataQueryDidUpdateNotification, however this update method doesn't seem to provide very reliable results all the time.

I was wondering if there is any way in which you can use an actual callback method to notify you when a file has been uploaded to iCloud? Or is the NSMetadataQueryDidUpdateNotification method the standard way of doing so?

Any hints and help would be appriciated :)

Thank you for your time :)

CodingBeagle
  • 1,888
  • 2
  • 24
  • 52
  • 1
    See the docs for `NSMetaDataItem` and `NSMetadataUbiquitousItemPercentUploadedKey` and `NSMetadataUbiquitousItemPercentDownloadedKey`. – rmaddy Nov 21 '12 at 18:03
  • Yes, I'm aware that you can retrieve these numbers from NSMetaDataItem, perhaps I should have mentioned that in the post :) My question was aimed at if this is the only way to figure out when a file has been uploaded? That is, using *NSMetadataQueryDidUpdateNotification* coupled with the *NSMetaDataItem* key. – CodingBeagle Nov 21 '12 at 19:15
  • Can you give a Flow level example how to achieve this I am struggling this to store and restore a Core Data Sqlite file from custom filter. I don't want to do conventional method of Core Data with iCloud but thought my code like wahtsapp. – codelover Jan 11 '16 at 08:20
  • @rmaddy I check the suggestion and also some docs but there is no clear tutorial or understand how this is achieved and i am unable to get the percentage than till getting is uploaded 0---1 but not 0.1,0.2...., can you help with an example. I am using iOS 7 onwards and Xcode 7 just FYI – codelover Jan 21 '16 at 17:11
  • @VickyDhas Have you got any success in making the progress similar to whatsApp progress of backup the chat to iCloud? please answer here, if you have something to share https://stackoverflow.com/questions/46274400/how-to-check-the-progress-of-the-items-being-uploaded-to-icloud – Bhavin Kansagara Sep 18 '17 at 08:44
  • What I learnt from there was that iCloud zipping file contents and extraction to consolidate files and expand is what whatsapp showing as progress bar. Not the icloud upload. As u may try to backup your huge video and image BU of whatsapp to icloud. download whatsapp on other phone and restore. If you ahve quit wahtsapp or phone on device 1 the other device will extract corrupt images. This concluded iCloud is always in background with its own time and priority to BU. – codelover Sep 25 '17 at 09:43

0 Answers0