Ios 在特定的持续时间后在线程上运行应用程序

Ios 在特定的持续时间后在线程上运行应用程序,ios,Ios,我正在运行一个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 NSU

我正在运行一个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
});
dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0)^{
//发布加载已开始的通知
对于(x=0;x++;x

有人建议吗?

首先创建一个计时器,如下所示:

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
    });
}
-(void)callWebService
{
调度异步(调度获取全局队列(调度队列优先级默认为0)^{
//发布加载已开始的通知
对于(x=0;x++;x

希望这有帮助!:)

您可以这样做:-
[NSTimer scheduledTimerWithTimeInterval:3600.0目标:自选择器:@selector(triggerTimer)用户信息:无重复:是]

并使用此方法调用您的web服务:-

- (void)triggerTimer
{
    // Call your web service from here.
    // Refresh here.
}

您需要关于在1小时后刷新应用程序的建议吗?为什么不使用
performSelector:withObject:afterDelay:
并从此处调用web服务?你可以把你的延迟时间放在里面。用一些
方法写上面的代码,然后用
NSTimer
调用
方法,间隔1小时。请看prachi,你可以使用计时器,60分钟后,调用该方法并重置计时器。@Piyushubey:没错!!!很高兴看到是的。