3

I just recently switched to AFNetworking to handle all my networking within my app. However, it now appears to be blocking the main thread so my MBProgressHUD won't spin until after the operation finishes and my pullToRefreshView will also not animate until after the operation. How would I fix this?

- (void)pullToRefreshViewShouldRefresh:(PullToRefreshView *)view; {

    // Call the refreshData method to update the table
    [dataController refreshData];
}


- (void)refreshData {

    NSURLRequest *request = [NSURLRequest requestWithURL:[FCDataController parserURL]];

    NSLog(@"URL = %@", request);

    AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request

    success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {

        _calls = [[NSMutableArray alloc] init];

        XMLParser.delegate = self;
        [XMLParser parse];
    }

    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {

        if ([delegate respondsToSelector:@selector(refreshDataDidFailWithError:)]) {
            [delegate refreshDataDidFailWithError:error];
        }

    }];
    [operation start];
}
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73

2 Answers2

1

By default, AFNetworking calls the success/failure blocks on the main thread (after the network operation runs on a background thread). This is a convenience for the common case where your code just needs to update the UI. If you need to do some more complex operation with the results (like parsing a big XML document), then you can specify some other dispatch queue on which your callback should be run. See the documentation for more.

Update (11 Feb 2016): AFNetworking has changed quite a bit in the nearly three years since I posted this answer: AFHTTPRequestOperation doesn't exist any more in the current version (3.0.4). I've updated the link so it's not broken, but the way you'd accomplish something similar these days is likely quite different.

Sixten Otto
  • 14,816
  • 3
  • 48
  • 60
0

Where is the MBProgressHUD being called? Are you using SSPullToRefresh or some other implementation. I'm writing very similar code on a current project and its working great.

- (BOOL)pullToRefreshViewShouldStartLoading:(SSPullToRefreshView *)view {
    return YES;
} 

- (void)pullToRefreshViewDidStartLoading:(SSPullToRefreshView *)view {
    [self refresh];
}

- (void)refresh {
    NSURL* url = [NSURL URLWithString:@"some_url_here"];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        // consume response

        [_pullToRefreshView finishLoading];
        [self.tableView reloadData];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    }];
    [operation start];

My guess is that - (void)pullToRefreshViewShouldRefresh:(PullToRefreshView *)view; { is being called from a background thread.

KyleStew
  • 420
  • 1
  • 5
  • 14