다음 코드는 앱이 종료되기 전에 서버에 전화를 겁니다. 내 문제는 코드가 가끔 작동한다는 것입니다. 대부분 그렇지 않습니다. 다음은 코드입니다.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
메소드로 들어 가지 않는 것 같습니다.
편집 : 기본적으로 내가 할 일은 서버에 사용자가 로그인 한 상태에서 강제로 앱을 종료하는 것입니다. 어떻게해야합니까?
아이디어가 있으십니까?