Joh*_*ock 5 iphone cocoa-touch web-services polling
我试图确定每分钟轮询一次web服务的最佳策略,解析返回的xml,然后更新存储在共享实例中的对象.此过程需要在单独的线程中运行,并且只要应用程序正在运行,它就会继续运行.
似乎我可以将所有代码调用Web服务并将xml解析为NSOperation,并在应用程序启动时将NSOperation添加到存储在app delegate中的NSOperationQueue.
在NSOperation的主方法中使用NSTimer是否正确,这样操作将无限期地每分钟循环一次?在那种情况下,NSOperation永远不会真正回归 - 这似乎是我想要的,但我不确定这是否是正确的思考方式.
我试图解决的问题当然非常普遍,所以我试图弄清楚实现它的正确方法.任何建议都非常感谢.
Bla*_*rog -1
我不会使用 NSTimer 来解决这个问题/设计。当应用程序启动时,我会从 AppDelegate 创建 NSThread。我会降低该线程的优先级。NSThread main 方法内部基本上是一个循环。
-(void)main {
while(true) {
// get raw data from url
// hash the result
// compare the hash to the last time
if (currentHash != lastHash) {
// post a notification to default center with the new data
lastHash = currentHash;
}
// sleep the thread sleepForTimeInterval
}
}
Run Code Online (Sandbox Code Playgroud)
您的 Model 对象将订阅来自线程的通知并解析新数据并更新 ivars。您的视图对象将使用 KVO 监听模型并显示任何更新/更改。
| 归档时间: |
|
| 查看次数: |
1216 次 |
| 最近记录: |