【问题标题】:CoreData objects not getting unfaultedCoreData 对象没有得到无故障
【发布时间】:2013-02-20 14:16:52
【问题描述】:

在 iOS6 应用程序中,我使用 CoreData 从 DB 获取 NSManagedObjects 并将它们显示在 tableViewCell 中。我的问题是,与初始滚动位置之外的单元格对应的所有对象都处于故障状态并且不会恢复活力。我看不到我的错误。

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

【问题讨论】:

    标签: core-data restkit


    【解决方案1】:

    好吧,我从来没有真正使用过你的方法,所以我不会说它是错误的,但我会说它很奇怪。我猜 Contact 是 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;
    

    试试吧,希望对你有帮助

    【讨论】:

    • [联系 objectsWithFetchRequest:fetchRequest];是 Restkit 0.10.x 的扩展。在 0.20 中,他们删除了该代码,开发人员现在必须使用 Core Data 语法。
    猜你喜欢
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2012-08-07
    • 2016-10-22
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多