How to “touch” a Core Data entity, to trigger NSFetchedResultsController?

前端 未结 1 1103
执笔经年
执笔经年 2021-01-16 13:42

Say you have a core data entity,

public class CDPerson: NSManagedObject

You get new info from the net ... a field changes,

p.n         


        
相关标签:
1条回答
  • 2021-01-16 14:15

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

    With an end result of the NSFetchedResultsController being told that the associated person object has changed and the cell should be re-drawn.

    0 讨论(0)
提交回复
热议问题