0

Im using a NSManagedObject as an attribute within my ViewController, declared like this:

@property(retain, nonatomic)NSManagedObject *person;

Im propagating the content of a fetch to a UITableView, when the user taps the contact he is looking for, this is what happens:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       self.person = [contacts objectAtIndex:indexPath.row];

Contacts contains the result of the fetch, which is done like this:

NSArray *contactsArray;
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSManagedObjectContext *context = [(OAuthStarterKitAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Contacts" inManagedObjectContext:context];



[fetchRequest setEntity:entity];
testForTrue = [NSPredicate predicateWithFormat:@"SOME CONDITION"];

[fetchRequest setPredicate:testForTrue];
    [fetchRequest setFetchLimit:10];



contactsArray = [[NSArray alloc]initWithArray:[context executeFetchRequest:fetchRequest error:&error]];

When the user taps the contact, self.person has that value, but when I try to use that value in another method it's nil, and the address is 0x000000. This only happens on iOS 5, on iOS 6 person has the value of the contact selected and I can use elsewhere.

Any ideas?

Thanks.

subharb
  • 3,374
  • 8
  • 41
  • 72

1 Answers1

0

Instead of getting the NSManagedObject, which is always dependant of the life cycle of the context I just stored de NSManagedObjectID and then get the NSManagedObject by using the function objectWithID. And it worked!

subharb
  • 3,374
  • 8
  • 41
  • 72
  • Sometimes you must invent something more clever than simply storing of ID. For example try to create some records and try to send any fetch request on each `rowForIndexPath:` and you will see how it is slow even with ids – Gargo Sep 20 '14 at 18:03