0

I have a core data setup that has 2 database entities. For the sake of names I'll call them Primary and Secondary. Secondary only belong to one Primary (relationship is setup). In my main view that lists the Primary objects in a table I retrieved them and put them in an PriObject Class which stores it's properties (including the managed object ID). The PriObject is then added to a mutable array (priArray), which is then used to fill the table with the data. All works ok so far. When I then click on the row I can log the PriObject.moID.

I can't figure out how to lookup that object in the database so I can then add Secondary objects to it. I can't do it by name because some Primary might have the same name.

I need to work out how to get an object back from either the URI or the ID. I have the ID so I can generate the URI if I need to.

Can't get my head around it at all and any examples I have found while looking don't cover what I need. What options are there?

EDIT: I am currently getting all my objects with the following.

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Primary"
                                              inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];

NSError *error;
NSArray *objects = [context executeFetchRequest:request
                                          error:&error];


if ([objects count] == 0) {
    NSLog(@"Nothing found");
} else {
    NSLog(@"Something found");
}

How can I change this for just the one using:

ObjectWithID:
Designer023
  • 1,902
  • 1
  • 26
  • 43

1 Answers1

2

Call the objectWithID: method on your NSManagedObjectContext instance to retrieve the instance.

As an aside, it seems like you are making things harder on yourself with this PriObject class, it seems to be a wrapper around your NSManagedObject instances, is that right? I'd just use NSManagedObject subclasses directly, personally.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • You have a point, but I am thinking that my class will make any repetitive things much easier to do, ie switching images on the table with if statements etc. I am probably making life harder for myself in all honesty, but since I find it complicated I wanted to keep each class as clean as possible. I will try with your suggested code and see how that goes. – Designer023 Nov 21 '12 at 09:54