gpt4 book ai didi

ios - iOS:在后台启动时,NSURLSession并非每次都运行(带有静默推送通知)

转载 作者:行者123 更新时间:2023-12-01 19:44:01 26 4
gpt4 key购买 nike

如果当应用程序在后台通过推送通知调用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];

(如果我在应用程序处于 Activity 状态时调用NSURLSession,那么没问题,它每次都可以工作)。

编辑:

我们可以在该视频中看到,当我按 1ST 时间时,它会启动NSURLSession,而没有回答。如果单击 2ND 时间,将显示 1ST 的答案,然后显示 2ND 一个的答案。
*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.

我在视频中放了一个演示:

Video

编辑:
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];
});

是什么原因导致该问题?

最佳答案

我认为您对函数的调用有问题。
在这种情况下,您将无法使用下载队列。

代替此,请使用dispatch_main

关于ios - iOS:在后台启动时,NSURLSession并非每次都运行(带有静默推送通知),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com