Question

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;

}
Was it helpful?

Solution

Try Using

self.tableView.indexPathForSelectedRow

Instead of

self.selectedRow.row

OTHER TIPS

I think whats happening is when tableviewcell is selected its performing segue operation first and saving the values in variable, so when it navigates for the first time it doesn't have any passing values to be displayed or perform any appropriate operation, but in second case as the values are already stored in temp variables they are passed. So the solution for it is performing segue operation programatically in tableviewdidselect row after self.selectedRow = indexPath as the values are now present it navigates to next view controller with the passing values and functions accordingly

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