Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Passing data via NSNotificationCenter not working

Hi!

I have a class (Class 1) that has a modal segue to another class (Class 2). I want the user to input data into Class 2 and when the user hits "save" it refreshes the collectionView on Class 1. I have been successful in getting the data to save locally, but cannot seem to figure out why it's not refreshing the collectionView on Class 1 when the modal is dismissed. If I quit the app and reopen it, the data appears, but it doesn't appear until I actually exit the app.

I thought that adding [self.collectionview reloadData] into a viewWillAppear would work...but it didn't. Then scouring the web led me to NSNotificationCenter. In class 1 I have:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSaved:) name:@"DataSaved" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];

- (void) dataSaved:(NSNotification *)notification{

    [self.collectionView reloadData];

}

and in Class 2 I have:

- (IBAction)save_wod_navbar:(id)sender {
//save to CoreData
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];
//dismiss modal view controller and go back to Class 1
}

Am I overlooking something?

Thanks!

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

When adding an observer you should do it in the viewDidLoad method so that it will be added only once. If your view controller is part of a navigation stack then it could trigger viewWillAppear multiple times and so it makes it a bad place for things like registering observers.

Secondly, right after adding an observer you are removing it which is probably why it's not working. Remove this line: [[NSNotificationCenter defaultCenter] removeObserver:self];

Thanks Amit

I made your edits, but it still doesn't seem to be refreshing the collectionView for some reason. From Class 1, I tap the "+" button to add information, a modal view pops up (Class 2). I enter the information and click the save button to save to CoreData and dismiss the modal to go back to Class 1. The new info still doesn't show up. But as I mentioned before...closing the app completely and restarting it shows the new information in the collectionView. Just for kicks, I added a test and had the "save" button push to a completely new view and that worked. So this leads me to believe that somehow the page isn't being reloaded eventhough I'm telling it to reload in the void statement. :-(

Thanks, sir.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

It depends how you are loading data into your table view. I'd recommend using the NSFetchedResultsController which will automatically update the table view if the data has changed.

Check out the Core Data tutorials for more details:

  1. Fetching Data - Part 1
  2. Fetching Data - Part 2

AWESOME!!!! Got it to work. Thank you so much, Amit Bijlani

Mr. Watkins, that bow tie is so clutch!

As you were.

hahah! thanks a lot, Colin.