简体   繁体   English

当应用程序在单独的线程中在iOS中处于活动状态时,定期从服务器轮询数据的最佳方法是什么?

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

I need to poll data from server periodically in my iOS application. 我需要在iOS应用程序中定期轮询来自服务器的数据。 I need to do it every 10 seconds in a thread, in order to keep the UI usable. 我需要在一个线程中每隔10秒执行一次,以保持UI可用。 This function will be fired when the user logs in. I'm thinking about using NSRunLoop with NSTimer to achieve this functionality, and maybe use AFNetworking to get JSON data. 当用户登录时,将触发此函数。我正在考虑使用NSRunLoopNSTimer来实现此功能,并且可能使用AFNetworking来获取JSON数据。

Is this the correct approach? 这是正确的方法吗? Should this be done using GCD? 应该使用GCD吗?

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. 同意H2CO3,对于你的服务器来说,轮询可能会成为一个问题,而且客户端过多,但同时也同意你在任何情况下都不一定是错误的。

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. NSTimer定期开火。 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). 一旦发生火灾,一个方法(在主线程上)决定是否需要轮询(在你描述的情况下,如果在10秒的时间段内被调用,则总是'是')。 Form the request, NSURLConnection sendAsynchronousRequest: will move the slow part of the request off the main. 形成请求,NSURLConnection sendAsynchronousRequest:将请求的缓慢部分移出main。 The block on sendAsynch runs back on the main when the request is done. 当请求完成时,sendAsynch上的块会在main上运行。

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. 这可能与在sendAsynch块中执行表重新加载一样简单,或者更复杂,例如设置将在模型更改时触发的KVO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM