0

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?

Community
  • 1
  • 1
Sandy D.
  • 3,166
  • 1
  • 20
  • 31
  • What have you tried? How are you adding the data to the table view? Can you show all related code? – KerrM Dec 08 '14 at 15:51
  • you should look at `performSelectorOnMainThread` – sage444 Dec 08 '14 at 15:54
  • @KerrM All my parsing and retrieving the RSS Feed occur in a file that I import into my view controller. In my view controller, I do this: `self.rssNews = [[ParseRSSNews alloc] initWithUrl:self.url];` In initWithUrl method in the parser file, I did this: `[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:url];` Once it is done parsing, I did: `[self performSelectorOnMainThread:@selector(doneParsing) withObject:nil waitUntilDone:NO];` when I print out how many items are in my self.rssNews (in view controller), that it shows 0. So, it's not returning the data. – Sandy D. Dec 08 '14 at 16:02
  • As mentioned, you need to call reloadData on the *main thread*. – emma ray Dec 08 '14 at 16:43
  • @cdstamper Thank you! I figured it out =) – Sandy D. Dec 08 '14 at 18:21

1 Answers1

1

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]; });
});
Boris Charpentier
  • 3,515
  • 25
  • 28
  • Since my parsing and retrieving the RSS feed occur in a separate file, I don't have access to self.tableview. I was able to do this in my view controller: `[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; dispatch_async(dispatch_get_main_queue(), ^{ self.rssNews = [[ParseRSSNews alloc] initWithUrl:self.url]; [self.tableView reloadData]; });` However, the network indicator doesn't go while it's parsing. – Sandy D. Dec 08 '14 at 16:22
  • Thank you Boris!! This helped a LOT!!! I also added to turn off the networkActivityIndicatorVisible within the second dispatch_async(). Again, Appreciate all of your help!!! =) – Sandy D. Dec 08 '14 at 18:05
  • I'm guessing I would need to release the dispatch queue? I tried doing this in dealloc, `dispatch_release(backgroundQueue);` but Xcode says ARC forbids explicit message send of release -- so I guess ARC will handle releasing the dispatch queue and I shouldn't have to do anything for that? – Sandy D. Dec 08 '14 at 18:17
  • 1
    Found the answer: `If your deployment target is iOS 6.0 or Mac OS X 10.8 or later ARC will manage your queue for you. You do not need to (and cannot) use dispatch_retain or dispatch_release if ARC is enabled.` Reference: http://stackoverflow.com/questions/8618632/does-arc-support-dispatch-queues – Sandy D. Dec 08 '14 at 18:22