我有以下代碼在應用即將退出之前調用服務器。我的問題是,代碼有時起作用,有時不起作用。大部分不是。這裏是代碼:backgroundTask未在iOS中完成
//Set the user as in-active if app is force closed
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"called");
bgTask = [application beginBackgroundTaskWithName:@"setInActive" expirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"disp called");
//If the app is about to exit make the user inactive
[[APIManager sharedInstance] setInActiveOnCompletion:^(BOOL finished){
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
});
}
每次都會調用第一個NSLog。然而第二個沒有。這似乎應用程序甚至沒有進入dispatch_async
方法。
編輯:所以基本上我所需要做的就是告訴服務器,用戶強制退出應用程序,而該用戶登錄。我怎麼能這樣做?
任何想法?