定期从 NSURLConnection 接收数据
我正在使用 NSURLConnection 接收一些数据(JSON 格式)并在 connectionDidFinishLoading 方法中执行一些工作(例如将数据写入标签)。我想要的是,这应该发生,例如每两秒发生一次。即,接收数据>接收数据。将数据写入标签>等待2秒>接收数据... 换句话说,我想每两秒更新一次数据。当然,这应该在后台完成,以免阻止应用程序。
应该做什么?我是否需要 runloop、nstimer 或 NSOperation?我应该如何使用它们?
I am using NSURLConnection to receive some data (in JSON) and do some work (e.g. write data to a label) in connectionDidFinishLoading method. What I want is, this should happen e.g. every two seconds. That is, ReceiveData > WriteDataToLabel > Wait 2 seconds > ReceiveData ...
In other words, I want to update my data every two seconds. Of course, this should be done in background not to block app.
What should be done? Do I need runloop, nstimer or NSOperation and how should I use them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是“应该做什么?”
我相信答案是“不是这个”
不推荐从每台拥有您的设备的设备每两秒 ping 一次服务器。应用程序运行
Question is "What should be done ?"
I believe the answer is "Not This"
Not a recommended approach to be pinging the server every two seconds from every device that has your application running
好吧,我了解到您可以使用 NSTimer 来做到这一点。这很容易。
查看此链接:https://developer.apple .com/library/ios/#documentation/Cocoa/Conceptual/Timers/Timers.html
(你也可以考虑@Aaron的话)
Okey, I learned that you can do this using NSTimer. It is pretty easy.
Check out this link: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Timers/Timers.html
(You may also consider @Aaron's words)