从未调用完成处理程序?

时间:2013-11-27 08:49:01

标签: ios block

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

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

1 个答案:

答案 0 :(得分: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);
        }
    }];
}