gpt4 book ai didi

ios - 警告 : Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called

转载 作者:可可西里 更新时间:2023-11-01 06:13:11 27 4
gpt4 key购买 nike

我实现了 performFetchWithCompletionHandler(即:background fetch)从服务器下载一些数据。

为此,在 performFetchWithCompletionHandler 中,为了不阻塞主线程,我创建并启动了一个新任务(因此在后台线程中),当任务完成后,我调用了 performFetchWithCompletionHandler

给出的 CompletionHandler

但没关系,一旦调用 performFetchWithCompletionHandler,我就会在日志中立即收到(在下载任务完成之前)

Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.

所以我不明白该怎么做?这是否意味着所有的工作都必须在 performFetchWithCompletionHandler (所以在主线程中) 过程中完成并且 performFetchWithCompletionHandler 必须仅在所有作业完成,以这种方式阻塞主线程?

请帮助我理解如何正确地做

这是我的代码(它在 delphi 中,但它完全符合我之前描述的内容,没有我用 sleep(15000) 替换的网络连接):

class procedure TMainForm.performFetchWithCompletionHandler(self: id; _cmd: SEL; application: PUIApplication; completionHandler: id);
begin

aTaskID := SharedApplication.beginBackgroundTaskWithExpirationHandler(SynchLocationsTaskExpired);

//load the Profiles in other thread to not block the calling thread
fSynchLocationsTask := TThread.CreateAnonymousThread(
procedure
begin

Sleep(15000);


TThread.Queue(nil,
procedure
begin
ExecutePerformFetchCompletionHandler(completionHandler, UIBackgroundFetchResultNewData); // ==> here the app crash like completionHandler is not valid anymore
SharedApplication.endBackgroundTask(aTaskID);
end);

end);
fSynchLocationsTask.FreeOnTerminate := False;
fSynchLocationsTask.Start;

// => here i receive in log Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called

end;

最佳答案

- (void)application:(UIApplication*)application performFetchWithCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"Started background fetch");

// Create URL session based on NSURLSession object which is required for all background fetch operations.
MyUrlSession* urlSession = MyUrlSession.new;

// Update is asynchronous so we have to tell to application that
// we will call completion handler asynchronously. It prevents showing warning
// that completion handler was never called.
UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithName:@"MyBgTask" expirationHandler:^{
// This will cancel update and will call completion handler with error.
NSLog(@"Expiration handler for background fetch");
[urlSession cancel];
}];

// Start URL session with completion handler.
[urlSession startWithCompletionHandler:^(BOOL successful, NSString *errorString) {
NSLog(@"Background fetch completed with success = %d, error = %@", successful, errorString);
[application endBackgroundTask:backgroundTaskIdentifier];
completionHandler(successful ? UIBackgroundFetchResultNewData : UIBackgroundFetchResultFailed);
NSLog(@"Background fetch has ended.");
}];
}

关于ios - 警告 : Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45252962/

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