I have a project with iCloud sync, but it doesn’t work correctly at all. At first, I get an array of changed objects, then I convert them to CSV strings (which will be the contents of UIDocuments in cloud) and then I upload them to iCLoud. If I have less than 400 objects, everything is OK, but if I have more – app hangs.
I’ve tried to use local autorelease pool, I’ve split up large array to smaller ones. But it didn’t help.
What is the best way for uploading a lot of UIDocuments into iCloud?
My uploading method:
- (void)pushChangesToCloud:(NSArray *)changedObjects {
for (ObjectClass *someObject in changedObjects) {
NSURL *pathToSyncDoc = [[self urlForDocumentsFolderIniCloud] URLByAppendingPathComponent:someObject.name];
CustomUIDocument *syncDoc = [[[CustomUIDocument alloc] initWithFileURL:pathToSyncDoc] autorelease];
// converting object to CSV string
NSString *csvContents = [SomeClass convertObjectToCSV:someObject];
syncDoc.documentContent = csvContents;
[syncDoc saveToURL:[syncDoc fileURL]
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
NSLog(@"YAY!");
} else {
NSLog(@" D: ");
}
}];
}
}
Thanks in advance and sorry for my English.
I finally figured out my problem, thanks to @Eimantas for his help. He was right about overloaded queue. Since “saveToUrl:forSaveOperation:completionHandler:” is i/o operation, queue created a lot of threads (for each UIDocument) and app hangs.
I overloaded “saveToUrl:forSaveOperation:completionHandler:” method in my custom UIDocument (to avoid saving docs in concurrent threads):
Then I used my own serial queue to execute all save operations.
At first, you need to add it like an iVar:
Then create it in init method:
use it for saving:
and then release it in dealloc:
After this changes I don’t have any problems with it.
Hope this helps! (and sorry for my English :>)