Search code examples
iosiphonetableviewrelationships

Segue doesn't push new property data until 2nd push?


So I am working on learning more about Core Data and am up to the point where I am trying to use subclassing to work with relationships. I want to be able to eventually do a slightly advanced app with the following type of view scheme: Tab View -> Navigation View -> Table View -> Table View -> Detail View. I got everything working (though I am sure I am mixing some fetchedresultscontroller type calls with managedobjectcontext subclass calls) and my 2nd table view shows the list of child objects of the parent table, though here is my issue.

If I go back to the parent and select a new parent object, the new object isn't pushed to the child table view and the old children are shown until i press the back button and re-push the view again. Here is my code from the parent table and child table view controllers and I already know that they are "denormalized" in nature as there are redundant calls, I am just trying to find something that works.

I worked off of the Apple Recipes app and am using the CoreDataHelper module found at http://maybelost.com/2010/12/core-data-helper-revised/

ATPTestTableViewController.h

{   NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *objectList;

    FACTypes *facTypes;
    FACTypes *selectedFACType;
}

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *objectList;
@property (nonatomic, retain) FACTypes *selectedFACType;
@property (nonatomic, retain) NSIndexPath *selectedRow;

ATPTestTableViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.managedObjectContext = [(ATPAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = self.managedObjectContext;

objectList = [CoreDataHelper getObjectsForEntity:@"FACTypes" withSortKey:@"number" andSortAscending:YES andContext:self.managedObjectContext];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedFACType = self.objectList[indexPath.row];
self.selectedRow = indexPath;

}

-(NSFetchedResultsController *) fetchedResultsController {
    // Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
        // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FACTypes" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

}

return fetchedResultsController;

}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSString *segueIdentifier = [segue identifier];
if ([segueIdentifier isEqualToString:@"showDetail"]) // This can be defined via Interface Builder
{

    NSLog(@"loading segue");

    self.selectedFACType = self.objectList[self.selectedRow.row];

    ATPDetailTestTableViewController *vc = [segue destinationViewController];
    vc.managedObjectContext = self.managedObjectContext;
    vc.facType = self.objectList[self.selectedRow.row];
    NSLog(@"FACType: %@", self.selectedFACType.title);
}
}

ATPDetailTestTableViewController.h

{

    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *objectList;

    FACTypes *facType;
    NSMutableArray *facMinimums;
    NSMutableArray *minimums;
}

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *objectList;
@property (nonatomic, retain) FACTypes *facType;
@property (nonatomic, retain) NSMutableArray *facMinimums;

ATPDetailTestTableViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.managedObjectContext = [(ATPAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = self.managedObjectContext;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedMinimums = [[NSMutableArray alloc] initWithArray:[self.facType.minimums allObjects]];
[sortedMinimums sortUsingDescriptors:sortDescriptors];

self.facMinimums = sortedMinimums;
[self.tableView reloadData];

}

- (NSFetchedResultsController *) fetchedResultsController {
    // Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
        // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FACMinimums" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(self.facType == %@)", self.facType];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

}

return fetchedResultsController;

}

Solution

  • Try Using

    self.tableView.indexPathForSelectedRow
    

    Instead of

    self.selectedRow.row