NSManagedObjectContext没有看到对实体属性的更改

时间:2013-10-22 15:53:12

标签: ios core-data nsmanagedobjectcontext

我是Core Data的新手。

我的数据模型有一个User实体,该实体具有从服务器获取的属性fullName。我正在使用Users在表格视图中显示所有NSFetchedResultsController的列表。当所有Users更新fullName属性时,我希望MOC发送更改通知。但是,它没有这样做。因此,我的FRC也没有得到更新。

我已经清理并构建,修改了我的数据模型并构建甚至删除了User.h/m并重新生成它。我仍然无法弄清楚问题是什么。并且fullName实际上正在更新,我可以手动重新加载表视图并查看更改。我的问题是什么?

代码:

设置fullName

- (BOOL)methodName:(NSDictionary *)data
{
    self.fullName = data[@"fullName"];
    self.imageData = data[@"image"];
}

表视图:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.frc.sections[section] numberOfObjects];
}

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

return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
User *user = [self.frc objectAtIndexPath:indexPath];

cell.imageView.image = [UIImage imageWithData:user.imageData];
cell.textLabel.text = user.fullName;
}

设置FRC:

- (void)viewDidLoad
{
[super viewDidLoad];

if (!self.model) self.model = [XPModel sharedInstance];
[self.model addDelegate:self];

if (!self.frc) {
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([User class])];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"fullName" ascending:YES]];
    request.fetchBatchSize = 10;

    self.frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.model.moc sectionNameKeyPath:nil cacheName:nil];
    self.frc.delegate = self;
}

NSError *error;
[self.frc performFetch:&error];

NSAssert(!error, error.localizedDescription);  
}

FRC委托方法:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

UITableView *tableView = self.tableView;

switch(type) {

    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];  //  Only this is called, and only at the beginning
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];  //  Never called
        break;

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray
                                           arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray
                                           arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}

设置模型:

- (void)coreDataSetup
{
NSError *error;

NSURL *storeURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject URLByAppendingPathComponent:@"userdb.sqlite"];;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];

NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSAssert([storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error], error.localizedDescription)

self.moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.moc.persistentStoreCoordinator = storeCoordinator;
}

1 个答案:

答案 0 :(得分:1)

来自此委托回调的文档:

  

在更新事件期间可以多次调用此方法(例如,如果要在后台线程上导入数据并将其添加到批处理中的上下文中)。您应该仔细考虑是否要在收到每条消息时更新表格视图。

我建议你不要依赖这种机制。最好在导入期间禁用委托。只需在完成后保存并重新装入表格(或根据批量数量定期)。

我没有看到你在任何地方保存 - 你还应该在导入大量数据时定期这样做。