Search code examples
ioscore-dataios6uitableview

How do you pass core data corresponding to a row selected in a table?


I am learning how to use Core Data. I have an app that fills out all the variable of an entity labeled User. I then take these users and load them to a table. At this point the users can be selected and pushed to a new view controller at which point I generate a PDF file of the user selected. So I think I am misunderstanding what it is I have to pass to the view controller for it to access the core data selected in the table. Here is what I have in my table view controller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.spinner startAnimating];
    ReportPDFViewController *reviewViewController = [[ReportPDFViewController alloc] init];
    reviewViewController.userInfo = [[self.fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:reviewViewController animated:YES];   
}

Then the next view states this

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Email PDF"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(emailPDF)];

    self.navigationItem.rightBarButtonItem = barButton;

    TrafficSignalProAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    context = [appDelegate managedObjectContext];

    // Do any additional setup after loading the view.

    NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entitydesc];


    NSError *error;
    NSArray *matchingData = [userInfo executeFetchRequest:request error:&error];

    NSString *Intersection;
    NSString *currentDay;
    for (NSManagedObject *obj in matchingData) {
        Intersection = sendIntersection;
        currentDay = sendDate;
    }



    NSString* fileName = [self getPDFFileName];

    [PDFRenderer drawPDF:fileName intersectionSearch:Intersection dateSearch:currentDay];

    [self showPDFFile];
    self.navigationItem.title = @"Report";

}

So I'm trying to pass the NSManagedObjectContext of the selected row to then load. I am really lost after that. I'm not sure if passing the managed object context is right and if it is I don't know what is wrong with the code in the ReportPDFViewController. I have looked through all the tutorials I can find. I have a limited programming background so any help would be greatly appreciated.


Solution

  • reviewViewController.userInfo = [[self.fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row];
    

    This sets userInfo to an object of type NSManagedObject (or a subclass).

    NSArray *matchingData = [userInfo executeFetchRequest:request error:&error];
    

    This is using userInfo as if it's a NSManagedObjectContext. I would imagine you get an invalid selector error here.

    What is the actual type of the userInfo attribute? It should be NSManagedObject.

    You do not need to do a fetch request in your viewDidLoad. Core Data is not a database. You do not always need to do a fetch request every time you want some information. Once you already have a managed object, you can get information related to it without a fetch request. If you've set up a custom class for it, you can treat it almost like it's an regular objective-C object.

    for (NSManagedObject *obj in matchingData) {
        Intersection = sendIntersection;
        currentDay = sendDate;
    }
    

    This code just doesn't make sense. You're looping, but each time through you're assigning the same value to the variables. I don't know what that value is, since sendIntersection and sendDate are not referred to anywhere else in the code you posted. In any case you're not using the results of the fetch request at all.

    I'm going to make a wild guess at what you need to do:

    Intersection = [userInfo valueForKey:@"intersection"];
    currentDay = [userInfo valueForKey:@"date"];
    

    It's a total guess, because I don't know what your data model is. No loop is needed, since you only want and have one user object.