从未调用完成处理程序?

问题描述 投票:3回答:1

当这个方法(application:didReceiveRemoteNotification:fetchCompletionHandler:)完成后,现在应该如何调用completionHandler块?

正如文档描述的那样“在实践中,您的应用程序应该在下载所需数据后尽快调用处理程序块。”

ios block
1个回答
7
投票

例如:

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler 
{
    // Based on the provided meta data in userInfo, load the resource from our 
    // server which corresponds to the "updated" information:

    NSDictionary* params = ; // based on userInfo
    [self fetchInfoWithParams:params completion:^(NSData* result, NSError* error){
        UIBackgroundFetchResult fetchResult;
        if (error) {
            fetchResult = UIBackgroundFetchResultFailed;
        }
        else if (result) {
            fetchResult = UIBackgroundFetchResultNewData;
        }
        else {
            // data is nil
            fetchResult = UIBackgroundFetchResultNoData;
        }

        // call the handler (if any):
        if (handler) {
            handler(fetchResult);
        }
    }];
}
© www.soinside.com 2019 - 2024. All rights reserved.