views:

170

answers:

1

Guys,

I have a ConfiguracaoDaApp class in my project that is a NSManagedObject subclass. I didn't change the default code that XCode generates.

I declare a instance variable of that type in my app delegate and in my appDidFinishLaunching method, I have been try to assign it's value from a object retrieved from the database like this:

    NSFetchRequest      *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity  = [NSEntityDescription entityForName:@"ConfiguracaoDaApp" inManagedObjectContext:self.managedObjectContext];

    [request setEntity:entity];
    configDaApp = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];

The problem is that the line

[[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];

don't returns a object of the type ConfiguracaoDaApp.

I tried change the line to this:

configDaApp = [[[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0] entity];

Then a NSEntityDescriptor is returned and the problem remains the same.

So, my question is: how to retrieve a real business object from a executeFetchRequest?

Thanks in advance.

Obs: forgive me if it is a beginner question but is my first iPhone app.

A: 

I just figure out what is happening:

[[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];

returns a autorelease object and when I tried to access the property in other parts of the code, its contents was memory trash of the application because the original ConfiguracaoDaApp object was released by autorelease pool. My property was declared with retain, but the object is autoreleased anyway. So I putted explicitly a retain in the line:

configDaApp = [[[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0] retain];

Then everything works fine.

Thanks anyway, guys.

R31n4ld0_
Just don't forget of release in dealloc. :D
R31n4ld0_