Question

In my nooby quest to learn how to properly load data into a grouped table view from Core Data, I built a very simple little two-view app. I use MagicalRecord as my interface with CD.

The current problem is that, while input data is being saved to my store as expected, the table view isn't refreshed upon saving to display the new data. However, if I stop the app in the simulator, then restart from Xcode, the new data is displayed in the tableview.

I'm using NotificationCenter rather than delegation for the communiation. Here is the code from my initial View Controller (containing and delegate to the tableview), followed by the code from the AddViewController, used to add data. Can someone with MagicalRecord experience please shine a light on me? Thanks!

//
//  ViewController.m
//  MRGroupingTest
//
//  Created by Tim Jones on 1/9/14.
//  Copyright (c) 2014 TDJ. All rights reserved.
//

#import "ViewController.h"
#import "ListActivity.h"

@interface ViewController ()

{
    NSFetchedResultsController *frc;
}


@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self refreshData];
    frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationNewActivityAdded:) name:@"newActivityAdded" object:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) notificationNewActivityAdded:(NSNotification*)notification
{
    [self.myTableView reloadData];
}



#pragma mark Table View data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[frc sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id<NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];
    return rows;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
    return [sectionInfo name];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    if ([frc sections] > 0)
    {
        ListActivity *activity = [frc objectAtIndexPath:indexPath];
        cell.textLabel.text = activity.name;

    }

    return cell;

}

-(void) refreshData
{

    frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];

    [self.myTableView reloadData];
}


#pragma mark Table View delegate

@end

And here's the code from AddViewController:

//
//  AddViewController.m
//  MRGroupingTest
//
//  Created by Tim Jones on 1/9/14.
//  Copyright (c) 2014 TDJ. All rights reserved.
//

#import "AddViewController.h"

@interface AddViewController ()

@end

@implementation AddViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)saveAction:(UIBarButtonItem *)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    ListActivity * thisActivity = [ListActivity MR_createInContext:localContext];
    thisActivity.name =  self.activityField.text;
    thisActivity.category = self.categoryField.text;
    [localContext MR_saveToPersistentStoreAndWait];
    //Inform app
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
    //dismiss view
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];


}

- (IBAction)cancelAction:(UIBarButtonItem *)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
    //dismiss view
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}


@end
Was it helpful?

Solution

You need to reload your TableView data, i.e. Call the reloadData function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top