开发者

How to retrieve a business object from the return of executeFetchRequest method?

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 开发者_C百科app.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜