我有xml http://weather.yahooapis.com/forecastrss?w=20070458&u=c,我希望當xml更新時我的數據也得到更新。如何更新一段時間後的解析數據
謝謝。
我有xml http://weather.yahooapis.com/forecastrss?w=20070458&u=c,我希望當xml更新時我的數據也得到更新。如何更新一段時間後的解析數據
謝謝。
正如你可以看到這個XML有ttl
節點,它告訴生存時間是60秒。因此,您可以定期(根據TTL值在一分鐘內檢查一次)檢查此URL並保持最新狀態。
我們如何檢查
你可以解析一個XML(使用http://www.xcode- tutorials.com/parsing-xml-files/ tutorial)並獲取ttl節點的值。 – fedosov 2012-07-12 13:14:13
我只是想補充一點,你可以在你的應用程序中指定ttl值爲60秒,因爲這是恆定的數字(很可能是這樣)。 – fedosov 2012-07-12 13:16:08
請閱讀本教程xmlparser和NSXMLParser Class Reference。我認爲這對你會有所幫助。
您可以對此進行投票。
static void timerHandler(CFRunLoopTimerRef timer, void *info)
{
//request the xml here and compare it with the previous one
}
- (void)weatherMonitor
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CFRunLoopTimerContext context = {0, self, NULL, NULL, NULL};
CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent(), 1, 0, 0, timerHandler, &context);//use your own time interval
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
CFRelease(timer);
CFRunLoopRun();
[pool drain];
}
在後臺線程中運行weatherMonitor。
在哪裏寫檢查定時器。謝謝 – krishnendra 2012-07-12 13:14:20
您是否嘗試過週期性地檢查此URL以進行更改? – fedosov 2012-07-12 12:56:47
歡迎來到Stack Overflow。請花時間閱讀[常見問題](http://stackoverflow.com/faq),尤其是關於提問的問題。 – 2012-07-12 12:59:15
那麼你需要每2到5分鐘打一次url並解析接收到的數據。然後,當你的xml更新你的數據時也會得到更新 – 2012-07-12 13:14:00