iOS:块内的UIBackgroundFetchResult完成处理程序

时间:2015-02-05 23:12:19

标签: ios objective-c objective-c-blocks

可以在这里使用一些帮助,因为我对块和完成处理程序的理解非常有限。

我正在尝试在iOS中实现后台提取,同时遵循本教程并根据需要更改内容:http://www.appcoda.com/ios7-background-fetch-programming/

我已经实现了必要的前驱来启用后台提取,并在我的viewController中实现了以下方法,并验证了在模拟后台提取时它被触发:

- (void)retrieveMessagesInBackgroundWithHandler:(void (^)(UIBackgroundFetchResult))completionHandler

在我的方法中,我正在调用一个类SoapRequest来执行对Web服务的异步调用,并且该完成处理程序我能够确定是否有新数据。最后,我想发回UIBackgroundFetchResult值:

SoapRequest *sr = [SoapRequest createWithURL:[NSURL URLWithString:kServiceURL] soapAction:soapAction postData:soapEnvelope deserializeTo:[NSMutableArray array] completionBlock:^(BOOL succeeded, id output, SoapFault *fault, NSError *error) {

    if( !succeeded ) {
        NSLog(@"method failed: %@", methodName);
        completionHandler = (UIBackgroundFetchResultFailed);
    } else {
        //NSLog(@">>>>>> OUTPUT: %@", output);
        NSDictionary *responseDictionary = output;
        id response = responseDictionary[@"RetrieveMessagesResponse"][@"RetrieveMessagesResult"][@"a:MessagesResult"][@"b:MessageLabel"];
        if ([response isKindOfClass:[NSArray class]]) {
            NSArray *array = response;
            completionHandler = (UIBackgroundFetchResultNewData);
        } else {
            NSLog(@"Nothing new");
            completionHandler = (UIBackgroundFetchResultNoData);
        }
    }
}];

我的问题,正如你可以想象的那样,我正在尝试在块内部设置completionHandler。我收到错误: 变量不可分配(缺少__block类型说明符)

我真的不确定如何正确实现这一点,并希望获得一些见解。任何帮助将不胜感激。

提前致谢!!

1 个答案:

答案 0 :(得分:1)

您不应该分配完成块,您应该执行它并传递参数:

completionHandler(UIBackgroundFetchResultNoData);

请注意,为了安全起见,您还应该检查completionHandler是否为零,因为它会崩溃。

相关问题