我正在运行一个Web服务来刷新我的应用程序,但我希望在1小时后刷新我并且我使用代码来运行线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// post an NSNotification that loading has started
for (x = 0; x++ ; x < numberOfRequests) {
// create the NSURLRequest for this loop iteration
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
// do something with the data, response, error, etc
}
// post an NSNotification that loading is finished
});
有人可以建议吗?
答案 0 :(得分:0)
首先创建一个计时器,如下所示:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3600.0 target:self selector:@selector(callWebService) userInfo:nil repeats:YES];
注意: 3600.0
代表相当于1小时的秒数。
然后,您可以使用callWebService
这样的方法将您的代码包装起来,这就是我使用过的方法:
- (void)callWebService
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// post an NSNotification that loading has started
for (x = 0; x++ ; x < numberOfRequests) {
// create the NSURLRequest for this loop iteration
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
// do something with the data, response, error, etc
}
// post an NSNotification that loading is finished
});
}
希望这有帮助! :)
答案 1 :(得分:0)
你可以这样做: -
[NSTimer scheduledTimerWithTimeInterval:3600.0 target:self selector:@selector(triggerTimer) userInfo:nil repeats:YES];
用这种方法致电您的网络服务: -
- (void)triggerTimer
{
// Call your web service from here.
// Refresh here.
}