我想在一段时间后创建调用 web 服务的自定义视图并自行更新。即使应用程序未处于活动状态,数据也应该更新。那么最好的方法是什么?
user2043155
问问题
623 次
3 回答
1
使用NSTimer
,但当应用程序处于后台模式时,数据不会更新。应用程序激活NSTimer
后将继续工作。
于 2013-05-28T12:15:47.313 回答
0
你可以使用NSTimer
它。在您的自定义方法中添加您的代码并像下面这样调用该方法..
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(yourMethodName:) userInfo:nil repeats:YES];
在scheduledTimerWithTimeInterval
Parameter 中,您可以以秒为单位设置时间,以便selector
每 3 秒调用一次传入 Parameter 的方法。
NSTimer
从这个链接查看一个示例和教程nstimer-tutorial-creating-clockstopwatchtimer-iphoneipad
更新:
如果你想调用网络服务,那么你可以NSThread
像下面这样使用......
- (void) runTimer
{
[NSThread detachNewThreadSelector:@selector(updateAllVisibleElements)toTarget:self withObject:nil];
}
- (void) updateAllVisibleElements {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(service == nil)
{
service = [[WebService alloc] init];
}
[service getlocationID:currentLatitude andlongitude:currentLongitute];
[pool release];
}
请参阅此处的链接
于 2013-05-28T12:04:48.500 回答
0
您将能够跟踪位置,但当应用程序处于后台时,您将无法连接到您的网络服务。
于 2013-05-28T12:14:05.027 回答