简体   繁体   中英

Completion handler was never called?

When this method ( application:didReceiveRemoteNotification:fetchCompletionHandler: ) is done, now how should I call the completionHandler block?

As the document describes that "In practice, your app should call the handler block as soon as possible after downloading the needed data."

For example:

- (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);
        }
    }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM