2013-02-20 59 views
3

在iOS6應用程序中,我使用CoreData從數據庫中獲取NSManagedObjects,並將它們顯示在tableViewCell中。我的問題是,與初始滾動位置之外的單元格相對應的所有對象都處於故障狀態,並且不會恢復原狀。我看不到我的錯誤。CoreData對象沒有受到損害

fetchRequest.returnsObjectsAsFaults = NO;有幫助,但我想要一個乾淨的解決方案。

下面是代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ContactsCell"; 
    ContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    Contact *contact = [self.contacts objectAtIndex:indexPath.row]; 

    //here some contacts are faulted. contact.name is null if I fetch it 

    [cell setContactData:contact]; 

    return cell; 
} 

這裏是我取(用Restkit 0.10.3):

NSFetchRequest *fetchRequest = [Contact fetchRequest]; 
    fetchRequest.sortDescriptors = [self sortDescriptors]; 
    fetchRequest.returnsObjectsAsFaults = NO; 
    return [Contact objectsWithFetchRequest:fetchRequest]; 

回答

0

好吧,我從來沒有真正使用你的方法,所以我不會說這是錯誤的,但我會說這很奇怪。我猜聯繫人是NSManagedObject的一個子類,我可以相信它知道他最初獲取的獲取請求,並且他知道他被獲取的上下文,但只有在他已經獲取之後。我真的不明白他怎麼知道這些事情,如果他以前從來沒有從持久性商店中獲取的話。所以我建議你使用經典的executeFetch或fetchedResultsController來填充你的tableView。

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; 
[context setPersistentStoreCoordinator:persistentStoreCoordinator]; 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context]; 
fetchRequest.entity = entity; 
fetchRequest.sortDescriptors = [self sortDescriptors]; 
NSArray *array = [context executeFetchRequest:fetchRequest error:&error]; 
return array; 

試試吧,希望它有助於

+0

[聯繫objectsWithFetchRequest:fetchRequest]。是Restkit 0.10.x的擴展。在0.20版本中,他們刪除了這些代碼,開發人員現在必須使用Core Data語法。 – netshark1000 2013-12-02 10:02:56