2010-06-16 31 views
3

其实我工作的多任务处理和面对错误,请帮助我,因为我需要在后台应用苹果面对错误多任务处理代码

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIApplication* app = [UIApplication sharedApplication]; 

    // Request permission to run in the background. Provide an 
    // expiration handler in case the task runs long. 
    NSAssert(bgTask == UIBackgroundTaskInvalid, nil); 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     // Synchronize the cleanup call on the main thread in case 
     // the task actually finishes at around the same time. 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (bgTask != UIBackgroundTaskInvalid) 
      { 
       [app endBackgroundTask:bgTask]; 
       bgTask = UIBackgroundTaskInvalid; 
      } 
     }); 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     // Do the work associated with the task. 

     // Synchronize the cleanup call on the main thread in case 
     // the expiration handler is fired at the same time. 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (bgTask != UIBackgroundTaskInvalid) 
      { 
       [app endBackgroundTask:bgTask]; 
       bgTask = UIBackgroundTaskInvalid; 
      } 
     }); 
    }); 
} 
+0

我相信你所要做的只会在iOS 4.0中提供,而iOS 4.0仍然处于NDA之下。最好在iPhone开发人员中心的开发人员论坛上试试你的运气。 – 2010-06-16 23:48:25

回答

0

检查工作,并在前台应用了developer documentation,如果你有一个开发人员的帐户。我们无法真正回答os4.0的问题,因为它是在NDA之下,但您需要回答该问题的所有信息都在该页面上(一旦登录)。

+0

朋友我从指定的链接复制了这段代码,但是在执行时遇到了错误,以及如何定义bgtask,因为它显示了bgtask不在结构或联合中。 – user366584 2010-06-17 12:08:10

3

把你的@interface:

UIBackgroundTaskIdentifier bgTask; 

(出从现在NDA)。