【发布时间】:2013-05-28 12:02:11
【问题描述】:
我想创建自定义视图,在一段时间后调用 webservice 并自行更新。即使应用程序不处于活动状态,数据也应该更新。那么最好的方法是什么??
【问题讨论】:
-
您自己尝试过任何解决方案吗?
-
@MarcinKuptel 我已经与 NSTimer 合作过。但正如我所说,即使应用程序在后台,我也想更新数据。我不知道
我想创建自定义视图,在一段时间后调用 webservice 并自行更新。即使应用程序不处于活动状态,数据也应该更新。那么最好的方法是什么??
【问题讨论】:
使用NSTimer,但应用程序处于后台模式时数据不会更新。应用程序激活后NSTimer 将继续工作。
【讨论】:
您可以为此使用NSTimer。在您的自定义方法中添加您的代码并像下面这样调用该方法..
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(yourMethodName:) userInfo:nil repeats:YES];
在scheduledTimerWithTimeInterval 参数中,您可以设置时间(以秒为单位),这样您传入selector 参数的方法每3 秒调用一次。
查看NSTimer的一个示例和教程从此链接nstimer-tutorial-creating-clockstopwatchtimer-iphoneipad
更新:
如果你想调用 webservice 那么你可以像下面这样使用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];
}
查看链接Here
【讨论】:
您将能够跟踪位置,但当应用处于后台时,您将无法连接到您的网络服务。
【讨论】: