如何在iOS应用程序中管理定期维护?

时间:2013-03-09 05:58:47

标签: iphone ios objective-c scheduled-tasks freeze

我正在寻找一种能够在iPhone应用程序中处理定期维护的方法。我们在我们的应用程序中使用Web服务(.Net / C#),最近我们增加了服务器数量以维护网站上的流量。因此,每当网络团队上传网站时,他们正在显示网站上的定期维护。对于iPhone,我写了下面的代码来同时处理超时。

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];

它将在60.0秒后通知用户。所以用户抱怨冻结 iPhone应用程序。有没有其他方法来处理这个?或者在计划维护时是否有其他方法可以打开任何特定的屏幕?

任何帮助将不胜感激!感谢。

2 个答案:

答案 0 :(得分:1)

您需要在单独的线程上执行此代码。最简单的方法是使用Grand Central Dispatch,如下所示:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    NSURLResponse *response = nil;
    NSError *error = nil;
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
    NSData *resultData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];

    // Add code to handle returningResponse.

    // Error handling
    if (error != nil) {
        NSLog(@"An error occured! %@", error);
    }
});

答案 1 :(得分:1)

在这种情况下,iOS 7静音背景通知可能对您有用。