2

in my app, i need to call a web service to get data (http get). I am a new ios dev, i wonder if i should dispatch the http get call to background, and bring tableview reload data to foreground like the code below? thanks bunch!

-(void)updateDataInBackground {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {

        // hard work/updating here

        // when finished ...
        [self reloadTable];
    });
}

-(void)reloadTable {
       dispatch_async(dispatch_get_main_queue(), ^(void) {
            [myTableView reloadData];
        }); 
}
trillions
  • 3,669
  • 10
  • 40
  • 59
  • Why you need to run web services in background, cant you do same when application comes from background? – P.J Mar 04 '13 at 09:00
  • possible duplicate of [IOS Grand Central Dispatch with callback method](http://stackoverflow.com/questions/13115657/ios-grand-central-dispatch-with-callback-method) – Carl Veazey Mar 04 '13 at 09:04
  • 1
    That code looks OK to me. BTW it's "foreground", not "front ground" ;-) – trojanfoe Mar 04 '13 at 09:04
  • This is a good approach... But from background to foreground thread you can use dispatch_sync – nielsbot Mar 04 '13 at 09:06
  • @P.J when the application starts, say if a user clicks on a button, i will need to navigate to another controller and initiate a web service call to get data; after this, i need to reload data for my uitableview. I didn't put the fetching data call to the background, but my coworker questioned me on why i didn't do this. So i posted this question and hope to get an answer on what is the right thing to do and what is not right and why not right. Thanks a lot! :) – trillions Mar 04 '13 at 09:06
  • If you do it in the foreground your app will hang while the request is made. – nielsbot Mar 04 '13 at 09:07
  • @nielsbot this is what i wonder. when a user needs to see some contents, shouldn't we want them to wait until we get or dont get the content? if we still allow user to click else where around, what's the benefit though? – trillions Mar 04 '13 at 09:09
  • hmm...@CarlVeazey i do think the thread you posted helps! I am reading it. Should i keep this thread so other people will find answer when they have the same or similar question? Thanks for your help, everyone! :) – trillions Mar 04 '13 at 09:11
  • @nielsbot i now wonder if anyone wrote some wrapper on making these kinda of foreground/background switches..hmm..thanks for help! i will think more on how to make a clean code change in the morning, there are bunch of web service calls here and there in the app :) – trillions Mar 04 '13 at 09:14
  • @nanshi If you run web services in background, this will drain device battery, which most of users never want.. – P.J Mar 04 '13 at 09:16
  • @nanshi If while coming from background, if its taking some time, you can show some activity control to alert user, same as what watsapp application does.. – P.J Mar 04 '13 at 09:17
  • @nanshi Your approach looks OK to me. – viral Mar 04 '13 at 10:28
  • 1
    @nanshi please do keep your question for that very purpose! Glad the link helped. – Carl Veazey Mar 04 '13 at 11:46
  • @PJ background here means "not main thread" not suspended app state. – nielsbot Mar 04 '13 at 15:58
  • 1
    @nanshi -- you might like AFNetworking or a similar toolkit. – nielsbot Mar 04 '13 at 15:59
  • @nielsbot - yeah, actually i am using AFNetworking :). Hahahah, I just realized I dont need to dispatch the service myself. I m new to ios, so..being silly. Thank you! :) – trillions Mar 04 '13 at 18:32

1 Answers1

3

For perfect and complete results use this code :

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        @autoreleasepool {
           //your webservice operations here
        }

    });
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Amit Bhavsar
  • 1,273
  • 12
  • 23
  • It works fine but is there any other way to run web service with more speed in background. – Pradumna Patil Jun 24 '15 at 09:45
  • As most of apps on app store use AFNetworking library for call web services. You can compare your speed after use it too. You can download AFNetworking from below url. https://github.com/AFNetworking/AFNetworking – Amit Bhavsar Jun 24 '15 at 11:34