David Beck
David Beck

Reputation: 10159

Notify NSFetchedResultsController of dynamic property change

I have a NSManagedObject with a dynamic property, based on a relationship. I have a NSFetchedResultsController that fetches a list of these objects. However when the dynamic property changes, while KVO notifications are send out, the NSFetchedResultsController never calls it's delegate to reload the tableView.

- (BOOL)read
{
    return self.lastMessage.read;
}

- (void)setRead:(BOOL)read
{
    [self.messages enumerateObjectsUsingBlock:^(TSMessage *message, BOOL *stop) {
        message.read = read;
    }];
}

+ (NSSet *)keyPathsForValuesAffectingRead
{
    return [NSSet setWithObject:@"lastMessage.read"];
}

How do I force a change to be registered for the managedObject?

Upvotes: 0

Views: 401

Answers (1)

Mundi
Mundi

Reputation: 80265

In the Core Data Programming Guide, there is a whole chapter about custom accessors. In essence, you are responsible for calling the notification methods.

If you want to implement your own attribute or to-one relationship accessor methods, you use the primitive accessor methods to get and set values from and to the managed object's private internal store. You must invoke the relevant access and change notification methods

Thus:

-(BOOL)read {
   [self willAccessValueForKey:@"read"];
   BOOL r = self.lastMessage.read;
   [self didAccessValueForKey:@"read"];
   return r;
}

-(void)setRead:(BOOL)read {
   [self willChangeValueForKey:@"read"];
   [self.messages enumerateObjectsUsingBlock:^(TSMessage *message, BOOL *stop) {
      message.read = read;
   }];
   [self didChangeValueForKey:@"read"];
}

Upvotes: 1

Related Questions