ltsChangeUpdate已触发而不是NSFetchedRe

ltsChangeUpdate已触发而不是NSFetchedRe

本文介绍了NSFetchedResultsChangeUpdate已触发而不是NSFetchedResultsChangeDelete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下列方式启动的NSFetchedResultsController:

I have a NSFetchedResultsController initiated in the following way:

NSEntityDescription *myEntity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:myEntity];
[fetchRequest setFetchBatchSize:10];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"wasDeleted == %@", [NSNumber numberWithBool:NO]]];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor, nil]];

myFetchedResults = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
myFetchedResults.delegate = self;
[myFetchedResults performFetch:nil];

一些事实:


  • wasDeleted是MyEntity的NSNumber属性(BOOL)

  • wasDeleted is an NSNumber property (BOOL) of MyEntity

如果我将此属性更新为 [NSNumber numberWithBool :YES] NSFetchedResultsControllerDelegate调用didChangeObject但触发 NSFetchedResultsChangeUpdate 而不是 NSFetchedResultsChangeDelete

If I update this property to [NSNumber numberWithBool:YES] the NSFetchedResultsControllerDelegate calls didChangeObject but firing NSFetchedResultsChangeUpdate instead of NSFetchedResultsChangeDelete.

但是,如果我弹出视图并再次启动获取的结果控制器,则该对象不再存在,显示 [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@wasDeleted ==%@,[NSNumber numberWithBool:NO]]]; 有效。

However, if I pop the view and init again the fetched results controller, the object is not there anymore, showing that [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"wasDeleted == %@", [NSNumber numberWithBool:NO]]]; works.

在NSFetchedResultsChangeUpdate之后,当调用cellForRow时,我打印wasDeleted并将其设置为YES。

After this NSFetchedResultsChangeUpdate, when cellForRow is called, I print wasDeleted and it's set to YES.

可能出错在这里?

(换句话说,如果对象仍然存在于上下文中但是不适合谓词,则可以调用NSFetchedResultsChangeDelete?)

(In other words, NSFetchedResultsChangeDelete can be called if the object still exists on the context however doesn't fit the predicate?)

推荐答案

我遇到了完全相同的问题。

不要使用 wasDeleted 作为您的属性名称。处理CoreData对象时,不应使用已删除的变体。

Do not use wasDeleted as your property name. You should not use variations of deleted when dealing with your CoreData objects.

我将我的属性重命名为隐藏,并将其用于谓词。一切都按预期工作。

I renamed my property to hidden, and used that for the predicate. Everything worked as you would expect.

这篇关于NSFetchedResultsChangeUpdate已触发而不是NSFetchedResultsChangeDelete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:30