Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS AFNetworking blocking main thread

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];
}
like image 321
Jon Erickson Avatar asked Jan 17 '26 21:01

Jon Erickson


1 Answers

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.

like image 196
Sixten Otto Avatar answered Jan 20 '26 13:01

Sixten Otto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!