2011-12-01 131 views
1

我正在实现iPhone应用程序。我通过在应用程序中使用异步请求从服务器下载数据。如果按下主页按钮,我的应用程序委托不会直接调用。从服务器完成数据下载后,应用程序委托方法正在调用。那么,如何在我的应用程序中获得立即的委托调用。当应用程序进入后台时,应用程序代理不会调用

任何帮助,可以体会到。

回答

1

您正在实施哪种委托方法?你能在这里发布你已经尝试过的代码吗?请注意,一旦您的应用程序移至后台,您只能运行短期任务以延长电池寿命。

这个例子是苹果公司提供:

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

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     // Clean up any unfinished task business by marking where you. 
     // stopped or ending the task outright. 
     [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, preferably in chunks. 

     [app endBackgroundTask:bgTask]; 
     bgTask = UIBackgroundTaskInvalid; 
    }); 
} 

如果要执行长正在运行的任务,需要特殊权限,即使如此,也只能执行动作的某个子集。他们在这里讨论:

iOS App Programming Guide: App States and Multitasking

相关问题