简体   繁体   English

如何从executeFetchRequest方法的返回中检索业务对象?

[英]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. 我的项目中有一个ConfiguracaoDaApp类,它是一个NSManagedObject子类。 I didn't change the default code that XCode generates. 我没有更改XCode生成的默认代码。

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: 我在我的应用程序委托和我的appDidFinishLaunching方法中声明了该类型的实例变量,我一直试图从这样的数据库中检索对象来为其赋值:

    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. 不要返回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. 然后返回NSEntityDescriptor,问题仍然存在。

So, my question is: how to retrieve a real business object from a executeFetchRequest? 所以,我的问题是:如何从executeFetchRequest检索真实的业务对象?

Thanks in advance. 提前致谢。

Obs: forgive me if it is a beginner question but is my first iPhone app. Obs:如果是初学者的问题,请原谅我,但是这是我的第一个iPhone应用程序。

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. 返回一个自动释放对象,当我尝试访问代码其他部分的属性时,其内容是应用程序的内存垃圾,因为原始ConfiguracaoDaApp对象是由自动释放池释放的。 My property was declared with retain, but the object is autoreleased anyway. 我的属性是用keep声明的,但是无论如何该对象都是自动释放的。 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. 谢谢,伙计们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM