Does the RESTOperation onCompletion block run on the iOS main/UI thread?

47 views
Skip to first unread message

Axel Niklasson

unread,
Apr 11, 2012, 6:03:54 AM4/11/12
to mobile-c...@googlegroups.com
First a big thanks to all contributors, great info source. This is my first post and I disclaim knowledge in all areas related to my question. I have googled and read the docs and I can guess the answer but would like to know for sure: Does the RESTOperation onCompletion block run on the iOS main/UI thread?

In my code I (possibly unnecessarily) make sure the UI stuff runs on the main thread, as in the below example. It's been running fine with or without the dispatch but that could just be luck. (On a side not it would also be interesting to know the "cost" of an unnecessary dispatch call. But that would just be a bonus.)

Thanks for reading,
Axel

    CouchDocument* doc = [self.database untitledDocument];
    RESTOperation* op = [doc putProperties:jsonPatient];
    [op onCompletion: ^{
        if (op.error)
            NSLog(@"Couldn't save the new item");
        else {
            NSLog(@"Item saved");
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.popSegue.popoverController dismissPopoverAnimated:YES];
                [self.tableView reloadData];
            });
        }        
    }];

Jens Alfke

unread,
Apr 11, 2012, 1:15:11 PM4/11/12
to mobile-c...@googlegroups.com

On Apr 11, 2012, at 3:03 AM, Axel Niklasson wrote:

Does the RESTOperation onCompletion block run on the iOS main/UI thread?

It runs on the same thread that initiated the RESTOperation. (That derives from the fact that NSURLConnection calls its delegate on the thread from which it was started.) 

So yes, if you do your work on the main thread, the onCompletion blocks run on the main thread too.

—Jens

Axel Niklasson

unread,
Apr 13, 2012, 9:36:30 AM4/13/12
to mobile-c...@googlegroups.com
Great, thanks!
Reply all
Reply to author
Forward
0 new messages