iOS:UIBackgroundFetchResult块内完成处理程序

问题描述:

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

我试图实现后台获取iOS中,同时与本教程以下,并根据需要改变的东西出来:http://www.appcoda.com/ios7-background-fetch-programming/

我已经实施了必要的前体获得背景取指令启用并已实现了以下方法在我的viewController和已验证它是模拟后台抓取时被解雇:

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

在我的方法,我打电话一类SoapRequest到了执行异步调用Web服务的nd完成处理程序,我能够确定我是否有新的数据。最后,我想回送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类型说明符)

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

在此先感谢!

你不应该分配完成块的,你应该执行它,并传递一个参数:

completionHandler(UIBackgroundFetchResultNoData); 

注意,为了安全起见,你也应该检查是否completionHandler是零,因为它会崩溃,如果它是。

+0

非常感谢您发现我的疏忽! – 2015-02-05 23:27:50