Search code examples
iosobjective-cuitableviewactivity-indicator

Data not loading in UITableView after parsing in background


I'm trying to implement an activity indicator when I parse my XML data.

I found this Activity indicator (spinner) with UIActivityIndicatorView and have it working. However, because I'm using performSelectorInBackground when it finishes parsing none of the data shows up in my tableview.

I have tried [self.tableview reloadData] and I've tried [self.view setNeedsDisplay] however, nothing seems to work.

If I leave the view and come back to it, all the data appears in the tableview cells.

Any ideas?


Solution

  • You should call

    [self.tableview reloadData]
    

    in the main thread (you can check in wich thread you'r in with [NSThread isMainThread])

    you can do

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //create background queue
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
    //dispatch in background
    dispatch_async(backgroundQueue, ^{
            //execute long operation in background thread
            self.rssNews = [[ParseRSSNews alloc] initWithUrl:self.url];
        //dispatch in main thread after long operation is finish
        dispatch_async(dispatch_get_main_queue(), ^{  [self.tableView reloadData]; });
    });