What is the best way to poll data periodically from server when app is active in iOS in a separate thread?

后端 未结 1 1797
礼貌的吻别
礼貌的吻别 2021-02-04 10:04

I need to poll data from server periodically in my iOS application. I need to do it every 10 seconds in a thread, in order to keep the UI usable. This function will be fired whe

相关标签:
1条回答
  • 2021-02-04 10:28

    Probably the only part that must be done off the main thread is the request itself. Deciding that you need a request and forming that request can be done without any fancy stuff...

    Agree with H2CO3 that polling might become a problem for your server with too many clients in the wild, but also agree with you that it's not necessarily a mistake in all cases.

    Setup a timer ...

    [NSTimer scheduledTimerWithTimeInterval:10.0
                                     target:self
                                   selector:@selector(timerFired:)
                                   userInfo:nil
                                    repeats:YES];
    

    Run a request ...

    - (void)timerFired:(NSTimer *)timer {
    
        NSURLRequest *request = // setup your request
        [NSURLConnection sendAsynchronousRequest:request 
                                           queue:[NSOperationQueue mainQueue]
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (!error) {
           // change my model in an observable way, or
           // if we're in a vc, change my model and update the UI
    
           // if we want to stop polling, [timer invalidate];
        }
    }];
    

    NSTimer fires periodically. Upon fire, a method (on the main thread) decides if it needs to poll (in the case you described, always 'yes' if it's called on a 10sec period). Form the request, NSURLConnection sendAsynchronousRequest: will move the slow part of the request off the main. The block on sendAsynch runs back on the main when the request is done.

    The trick is that other parts of your app need to be setup to observe changes in the model and update the views. This may be as simple as doing a table reload in the sendAsynch block, or more complex, like setting up KVO that will trigger as the model changes.

    0 讨论(0)
提交回复
热议问题