0

I have found a few questions regarding this topic but known seem to get my problem solved. I have an iPhone app that pulls and XML feed from a server, parses it, then displays the data in three UITableViews (sorting occurs at parsing). I currently have NSTimers to update the UITables once the data is done being parsed. I know there is a better way. I am trying to use the parser delegate method

In my Parser.m

    -(void)parserDidEndDocument:(NSXMLParser *)parser{
   NSLog(@"done Parsing, now update UITable in otherViewController");
    otherViewController *otherUpdatingtmp =[[otherViewController alloc] initWithNibName:@"otherViewController" bundle:nil];
    [otherUpdatingtmp updateTable];
    }

to trigger the updateTable method that is located on the otherViewController to reloadData in the table. My NSLog tells me my updateTable is firing in the otherViewController thread however I can not seem to get my TableView to update because its returned as NULL.

In my otherViewController.m

-(void)updateTable{

    [tabeViewUI reloadData];
    NSLog(@"update table fired. table view is %@.", tabeViewUI);

}

It's gotta be something small i've overlooked. Thanks in advance!

EDIT 7/24/12: Thanks to @tc. for turning me onto using [NSNotificationCenter]

in my parser.m

-(void)parserDidEndDocument:(NSXMLParser *)parser{
  NSLog(@"done Parsing, now update UITable's with NSNotification");

    [[NSNotificationCenter defaultCenter] postNotificationName:@"parserDidEndDocument" object:self];}

then in each of my UITableViews I added:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable) name:@"parserDidEndDocument" object:nil];

    }
    return self;

}

lastly:

 -(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

I hope this helps someone else. It certainly helped me! Thanks @tc.

kev
  • 2,306
  • 3
  • 26
  • 31
  • You forgot to stick it in the ivar? – tc. Jul 21 '12 at 01:42
  • @tc. what would i need to put in as ivar? I do have the '-(void)updateTable' in my otherViewController.h. I'm a little new and am not sure about the difference of '@interface otherViewController() -end' and '-implementation otherViewController' Would that have something to do with it? I don't understand by my NSLog works with my current setup but in the same block '[tabeViewUI reloadData];' doesn't? – kev Jul 24 '12 at 16:22
  • Ah, ignore my last comment. You're allocating a *new* `otherViewController` and updating its table even though it never gets displayed. You need to update the *existing* `otherViewController`. – tc. Jul 24 '12 at 17:02
  • @tc. that sounds correct because my NSLog is constently returning null for my UITableView. Which creating a new otherViewcontroller instance would explain the cause of my null returned table. How would I update my existing table? I've been using NSTimers to update my tables (I have 3) from their respective threads however updating via 'parserDidEndDocument' would be much more desired. – kev Jul 24 '12 at 18:14
  • Your `otherViewController` should be the `Parser`'s delegate. – tc. Jul 24 '12 at 18:17
  • My Parser is currently it's own delegate (in Parser.h). I would like to update all three of my tables with the parser DidEndDocument. Can all three controllers serve as the parser delegate to get the DidEndDocument? Or should I take a different approach? Thanks for your patience! – kev Jul 24 '12 at 18:29
  • Then you probably want to use NSNotificationCenter and have the VCs register for notifications appropriately. Don't forget to `[[NSNotificationCenter defaultCenter] removeObserver:self]` in `-dealloc`. – tc. Jul 24 '12 at 18:38
  • I don't know much about NSNotificationCenter, I'll have to read up. Thanks for the direction! – kev Jul 24 '12 at 18:46
  • @tc. The notifications are exactly what i needed!! works perfectly. I'm using ARC so I put `[[NSNotificationCenter defaultCenter] removeObserver:self];` in my `- (void)viewDidUnload`. ...updating my post to include my conclusion. – kev Jul 24 '12 at 23:40
  • **NO.** Put it in `-dealloc`, `-viewDidUnload` is not guaranteed to be called (it is *only* called automatically when your view is unloaded following a memory warning). Also (at least in some OS versions) UIViewController uses NSNotificationCenter to detect memory warnings, so you should never call `removeObserver:self` anywhere apart from `-dealloc`; if you call it anywhere else, use `-removeObserver:name:object:`. – tc. Jul 25 '12 at 12:18
  • @tc. what about ARC Rules? like "You cannot explicitly invoke dealloc...."? If I use `-removeObserver:name:object:` I could just put that in my `updateTable` method so once the table is updated then the observer immediately removed. That should work right? – kev Jul 25 '12 at 15:06
  • You cannot *invoke* (i.e. call) `-dealloc` but you can still *override* it, and in this case you really should: There is no guarantee that `-updateTable` (or *any other method*) will get called before `-dealloc`. – tc. Jul 25 '12 at 16:16
  • @tc. aaaah, gotcha. Updating post above. – kev Jul 25 '12 at 17:27

0 Answers0