2013-03-06 50 views
0

即时通讯使用NSManagedObject我的视图控制器中的属性,声明如下:NSManagedObject成为空

@property(retain, nonatomic)NSManagedObject *person; 

即时通讯传播的取到一个UITableView,当用户点击他正在寻找接触的内容,这是发生了什么:

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

联系人包含的获取结果,这是这样完成的:

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具有该值,但是当我尝试在另一个方法中使用该值时,该值为零,地址为0x000000。 这只发生在iOS 5上,在iOS 6上,人员具有所选联系人的价值,我可以在别处使用。

任何想法?

谢谢。

回答

0

不但得不到NSManagedObject,这总是取决于我只是存储德NSManagedObjectID上下文的生命周期,然后通过使用功能objectWithID得到NSManagedObject的。它的工作!

+0

有时你必须发明一些比简单存储ID更聪明的东西。例如,尝试创建一些记录并尝试在每个'rowForIndexPath:'上发送任何获取请求,并且即使使用ids,您也会看到它如何缓慢 – Gargo 2014-09-20 18:03:23