【问题标题】:NSManagedObject becomes nullNSManagedObject 变为空
【发布时间】:2013-03-06 16:02:17
【问题描述】:

我在 ViewController 中使用 NSManagedObject 作为属性,声明如下:

@property(retain, nonatomic)NSManagedObject *person;

我将获取的内容传播到 UITableView,当用户点击他正在寻找的联系人时,会发生这种情况:

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

Contacts 包含获取的结果,操作如下:

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]];

当用户点击联系人时,self.person 具有该值,但当我尝试在另一种方法中使用该值时,它为 nil,地址为 0x000000。 这仅在 iOS 5 上发生,在 iOS 6 上,人具有所选联系人的值,我可以在其他地方使用。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: ios5 nsmanagedobject


    【解决方案1】:

    我没有得到NSManagedObject,这总是依赖于上下文的生命周期,我只是存储了NSManagedObjectID,然后使用函数objectWithID得到NSManagedObject。它奏效了!

    【讨论】:

    • 有时您必须发明一些比简单存储 ID 更聪明的方法。例如,尝试创建一些记录并尝试在每个 rowForIndexPath: 上发送任何获取请求,您会看到即使使用 id 也很慢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2012-03-28
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多