Question

UIDocument's saveToURL:forSaveOperation:completionHandler: is suppose to be asynchronous (in documentation, it says it performs this action on a background queue), but my code has a 7-10 seconds lag when calling this.

Here's the code:

NSLog(@"saving image...");
[picDoc saveToURL:[picDoc fileURL] 
 forSaveOperation:UIDocumentSaveForCreating 
completionHandler:^(BOOL success) {


    dispatch_async(dispatch_get_main_queue(), ^{
        if (success) {
            NSLog(@"image save successful!");
            UIImage *thumbnail = [image imageByScalingAndCroppingForSize:CGSizeMake(146, 110)];
            Picture *picture = [[Picture alloc] initWithThumbnail:thumbnail filename:fileName];
            [[set pictures] addObject:picture];




            [self setupPictures];

            [scrollView scrollRectToVisible:((UIView *)[[scrollView subviews] lastObject]).frame animated:YES];
            currentIndex = [[set pictures] count] - 1;
            [self showPicture];

        } else {
            NSLog(@"failed saving image");
        }
        [SVProgressHUD dismiss];
    });

    [picDoc closeWithCompletionHandler:nil];
}];
NSLog(@"exit");

And the console:

2012-05-15 07:07:27.417 Picventory[5939:707] saving image...
2012-05-15 07:07:34.120 Picventory[5939:707] exit
2012-05-15 07:07:34.740 Picventory[5939:707] image save successful!

Why is there such a huge lag when the call is asynchronous? Thanks

Was it helpful?

Solution

Writing the file to disk is asynchronous, but taking the snapshot of the document's data is not. You should use the Time Profiler in Instruments to find out what is actually taking so long. I would guess that you might need to optimize your contentsForType:error: (or equivalent) method, but measure first!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top