iOS: NSURLSession 在后台启动时不是每次都有效(带有静默推送通知)
iOS: NSURLSession is not working everytime when launched in background (with silent push nofitication)
如果我在应用程序在后台使用推送通知时调用 NSURLSession,收到的响应可能只有一次三次。
NSLog(@"SEND: LAUNCH FUNCTION!!!!");
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:url_string]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if (error == nil) {
NSLog(@"SEND: RESPONSE FUNCTION GOOD!!!!");
NSMutableArray* responseArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
[self parseAndAddLovAll:responseArray toArray:self.objects];
});
}
else
{
NSLog(@"SEND: RESPONSE FUNCTION BAD!!!!");
}
}] resume];
(如果我在应用程序处于活动状态时调用 NSURLSession,没问题,它每次都有效)。
编辑:
我们可以在该视频中看到,当我按下 1ST 时间时,它会启动 NSURLSession,但没有得到应答。如果我点击2ND次,出现1ST的答案,接着是2ND[=35=的答案] 一个.
*SEND: LAUNCH FUNCTION!!!!* =>** launch the NSURLSession.
*SEND: RESPONSE FUNCTION GOOD!!!!*** => Response ok from the NSURLSession.
*SEND: RESPONSE FUNCTION BAD!!!!*** => Response not ok from the NSURLSession.
*SEND: FINISHED!!!!*** => Download execution finished with success.
我在视频中放了一个演示:
编辑:
Call of the function:
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{
ListOfValueSync * lovSync = [[ListOfValueSync alloc] init];
// Synchronization
BOOL ret = [lovSync getAllListOfValueAll];
});
什么可能导致该问题?
当应用程序处于后台时启动下载
To support this background mode, enable the Remote notifications
option from the Background modes section of the Capabilities tab in
your Xcode project. (You can also enable this support by including the
UIBackgroundModes key with the remote-notification value in your app’s
Info.plist file.)
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app.
使用后台执行
时要记住
Do minimal work while running in the background. The execution time
given to background apps is more constrained than the amount of time
given to the foreground app. Apps that spend too much time executing
in the background can be throttled back by the system or terminated.
我认为您对函数的调用有问题。
在这种情况下,您不能使用下载队列。
请改用 dispatch_main
如果我在应用程序在后台使用推送通知时调用 NSURLSession,收到的响应可能只有一次三次。
NSLog(@"SEND: LAUNCH FUNCTION!!!!");
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:url_string]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if (error == nil) {
NSLog(@"SEND: RESPONSE FUNCTION GOOD!!!!");
NSMutableArray* responseArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
[self parseAndAddLovAll:responseArray toArray:self.objects];
});
}
else
{
NSLog(@"SEND: RESPONSE FUNCTION BAD!!!!");
}
}] resume];
(如果我在应用程序处于活动状态时调用 NSURLSession,没问题,它每次都有效)。
编辑:
我们可以在该视频中看到,当我按下 1ST 时间时,它会启动 NSURLSession,但没有得到应答。如果我点击2ND次,出现1ST的答案,接着是2ND[=35=的答案] 一个.
*SEND: LAUNCH FUNCTION!!!!* =>** launch the NSURLSession.
*SEND: RESPONSE FUNCTION GOOD!!!!*** => Response ok from the NSURLSession.
*SEND: RESPONSE FUNCTION BAD!!!!*** => Response not ok from the NSURLSession.
*SEND: FINISHED!!!!*** => Download execution finished with success.
我在视频中放了一个演示:
编辑:
Call of the function:
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{
ListOfValueSync * lovSync = [[ListOfValueSync alloc] init];
// Synchronization
BOOL ret = [lovSync getAllListOfValueAll];
});
什么可能导致该问题?
To support this background mode, enable the Remote notifications option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the remote-notification value in your app’s Info.plist file.)
For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the relevant content and integrate it into your app.
使用后台执行
时要记住Do minimal work while running in the background. The execution time given to background apps is more constrained than the amount of time given to the foreground app. Apps that spend too much time executing in the background can be throttled back by the system or terminated.
我认为您对函数的调用有问题。 在这种情况下,您不能使用下载队列。
请改用 dispatch_main