在块中更新UILabel

时间:2013-12-13 13:21:49

标签: ios objective-c cocoa-touch

在iOS7应用程序中,我有以下代码,标签未更新,活动指示器也未隐藏。我不太熟悉iOS线程,但我认为AFNetworking块不会返回主队列。无论哪种方式,如果我删除dispatch_async线我仍然有相同的问题。有人可以帮我解决这个问题吗

[myClient getPath:urlMethodWithParameters parameters:nil
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

              [self processFullDataString: responseString];
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {


              dispatch_async(dispatch_get_main_queue(), ^{
                  [self hideActivityIndicator];

                  [self updateProgressLabelWithText:@"Unabke to downloading sync data ..."];

                  UIAlertViewAutoDismiss *alert = [[UIAlertViewAutoDismiss alloc] initWithTitle:@"Information" message: [NSString stringWithFormat: @"Could not load data from the PromoPortal server - %@", [error localizedDescription]] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil,nil];
                  [alert show];
                  });
              }];
}

3 个答案:

答案 0 :(得分:0)

应始终记录完成处理程序的执行上下文。从历史上看,AFN会在主线程上发送(虽然这不是恕我直言的最佳方式)。

此处,您有两个完成处理程序:成功处理程序和失败处理程序。您需要确保在每个完成处理程序中相应地更新UIKit元素。

答案 1 :(得分:0)

    NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    [self processFullDataString: responseString];

这不是在UI线程上调用的。您是否尝试过使用主队列块来包装它?

dispatch_async(dispatch_get_main_queue(), ^{
      NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
      [self processFullDataString: responseString];
};

答案 2 :(得分:0)

在失败栏中尝试此操作:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait