当应用程序在iOS中在单独的线程中处于活动状态时,定期从服务器轮询数据的最佳方法是什么? [英] What is the best way to poll data periodically from server when app is active in iOS in a separate thread?

查看:186
本文介绍了当应用程序在iOS中在单独的线程中处于活动状态时,定期从服务器轮询数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在iOS应用程序中定期轮询来自服务器的数据。我需要在一个线程中每10秒执行一次,以保持UI可用。当用户登录时将触发此函数。我正在考虑使用 NSRunLoop NSTimer 来实现此功能,也许可以使用 AFNetworking 来获取JSON数据。

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 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.

这是正确的做法吗?是否应该使用GCD完成?

Is this the correct approach? Should this be done using 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...

同意H2CO3,轮询可能会成为服务器问题太多在野外的客户,但也同意你在所有情况下都不一定是错误。

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];

运行请求...

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

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.

诀窍是需要设置应用程序的其他部分以观察模型中的更改和更新视图。这可能就像在sendAsynch块中执行表重新加载一样简单,或者更复杂,例如设置将在模型更改时触发的KVO。

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.

这篇关于当应用程序在iOS中在单独的线程中处于活动状态时,定期从服务器轮询数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆