I am trying to upload a local document folder to a remote iCloud folder. I wrote this method to iterate over an array of files in a local folder, check if they already exist, and if they do not exist, upload them to iCloud. Note. This code is executed in the background thread, not in the main thread.
//Get the array of files in the local documents directory NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSArray *localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]; //Compare the arrays then upload documents not already existent in iCloud for (int item = 0; item < [localDocuments count]; item++) { //If the file does not exist in iCloud, upload it if (![[iCloud previousQueryResults] containsObject:[localDocuments objectAtIndex:item]]) { NSLog(@"Uploading %@ to iCloud...", [localDocuments objectAtIndex:item]); //Move the file to iCloud NSURL *destinationURL = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",[localDocuments objectAtIndex:item]]]; NSError *error; NSURL *directoryURL = [[NSURL alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:[localDocuments objectAtIndex:item]]]; BOOL success = [[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:directoryURL destinationURL:destinationURL error:&error]; if (success == NO) { //Determine Error NSLog(@"%@",error); } } else { ... } }
When I run this code, For Loop works fine - I use the NSLog instructions to find out which file it downloads, and every file stored locally that is not already in iCloud should start the download. After completing the For Loop cycle, Iβll check which documents are now in iCloud using developer.icloud.com . Only one file (the sqlite file, which my application continues to do but never uses) from many saved local downloads in iCloud. Why is only one file loaded when used for loops?
When I use the same code to upload individual files (without a byte), they are automatically uploaded to iCloud. Why does Loop prevent files from loading? Is this due to the fact that the For Loop cycle continues to the next line of code, without waiting for the completion of the last process / line? What's going on here?
EDIT . Usually, when I upload large files to iCloud (without using the for loop), I see on the iCloud developer site that the file is waiting for download almost instantly. I am testing everything over WiFi and I have been waiting for a while, but nothing appears (except the sqlite file).
EDIT . I also check the availability of iCloud in another way, which then allows you to call this method if iCloud is available. I also made sure that I read the documentation and watched the WWDC video on the Apple website, however they are complex and do not provide much explanation for what I am trying to do.
EDIT : after revising the code above (adding an error function), now I get this error message in the log:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn't be completed. (Cocoa error 512.)" UserInfo=0x1f5dd070 {NSUnderlyingError=0x208ad9b0 "The operation couldn't be completed. (LibrarianErrorDomain error 2 - No source URL specified.)"}
This makes things even more confusing because one of the files loads successfully, while the others do not.