如何“触摸"一个核心数据实体,以触发NSFetchedResultsController? [英] How to "touch" a Core Data entity, to trigger NSFetchedResultsController?
问题描述
假设您有一个核心数据实体,
Say you have a core data entity,
public class CDPerson: NSManagedObject
您从网上获得了新信息...字段发生了变化,
You get new info from the net ... a field changes,
p.name = 'Debbie'
该"CDPerson
"项目被触摸",因此,
That CDPerson
item is "touched" and hence,
您的NSFetchedResultsController将触发,
完美.
但是说CDPerson
可以属于CDAddress
在核心数据中,CDAddress
没有 的更改不会触发CDPerson
但是.触摸地址后,您确实要触摸相关的人员项目,
But. When an address is touched, you DO want to touch the relevant person items,
,以便结果控制者将知道CDPerson
已更改.
so that results controllers will know CDPerson
has been changed.
一个垃圾解决方案,假设您有一个不重要的字段,
One rubbish solution, assuming you have an unimportant field,
let touch = p.creationDate
p.creationDate = touch
或者您可以添加一个Int64和
Alternately you could add an Int64 and
p.touchyInt = a random number, or increment, etc
这些是较差的解决方案
实际上是否有一种触摸"方式,一个核心数据实体,用于结果控制者?
Is there actually a way to "touch" a core data entity, for results controllers?
这对于传播相关实体的更改似乎至关重要.
This seems to be critical in propagating changes in related entities.
很难相信没有解决方案.
It's hard to believe there is no solution for this.
与PS相关的质量检查: NSFetchedResultsController,其关系未更新
PS related QA: NSFetchedResultsController with relationship not updating
关系未更新的NSFetchedResultsController"
推荐答案
Core Data通过触发-willChangeValueForKey:
和-didChangeValueForKey:
识别更改,如@pbasdf中的注释所示.如果愿意,您只能拨打-didChangeValueForKey:
,但我真的会两者都打电话.
Core Data recognizes changes by the firing of -willChangeValueForKey:
and -didChangeValueForKey:
as the comment from @pbasdf indicated. You can call only -didChangeValueForKey:
if you want but I would really do both.
这些呼叫也可以处理关系.因此,在您的地址"对象中,您可以触摸要更改的值和与人的关系:
Those calls also work on relationships. So in your Address object, you can touch both the value to be changed and the relationship to the person:
- (void)setCity:(NSString*)cityString
{
[self willChangeValueForKey:@"city"];
[self willChangeValueForKey:@"person"];
_city = cityString;
[self didChangeValueForKey:@"person"];
[self didChangeValueForKey:@"city"];
}
最终结果被告知NSFetchedResultsController
关联的人员对象已更改,因此应该重新绘制单元格.
With an end result of the NSFetchedResultsController
being told that the associated person object has changed and the cell should be re-drawn.
这篇关于如何“触摸"一个核心数据实体,以触发NSFetchedResultsController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!