Documents do not load in iCloud when using For Loops - iOS

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.

+6
source share
1 answer

OK, the last edit is helpful. The error reports that the source URL ( directoryURL in your code) is missing - probably because it is nil . Why will it be nil ? Because you are creating it wrong. You use -[NSURL initWithString:] , and the docs say that:

This string must match the format of the URL as described in RFC 2396. This method parses the URLString in accordance with RFC 1738 and 1808.

You are passing the path to the file, not the URL. If you pass NSURL something that it does not recognize as a valid URL, it usually returns nil . It appears that NSURL will often generate the file url, but failure is the expected behavior here. From what I can say, it looks like it works if the file name does not contain spaces, but it is not documented, and not something you could rely on.

What you need to do is change the line in which you initialize directoryURL to use what takes the file path. Sort of:

 NSURL *directoryURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:[localDocuments objectAtIndex:item]]]; 

Also, make sure directoryURL not nil , just in case.

+6
source

All Articles