I have a simple chat with typically relations chat and messages.
Chat entity have property chatId - type Integer
Message entity have property chatId - type Integer
In chat entity i created a fetched property (messagesFP) with simple (i think) predicate
chatId == $FETCH_SOURCE.chatId
I have chat with Id = 1, and messages which property chatId = 1.
But messagesFP - return empty array.
If i change predicate to
chatId == 1
So messagesFP return correct messages.
So, how to write correct predicate to fetch messages for current chat?
Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.
Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.
If $FETCH_SOURCE points to an NSManagedObjectID
on your end, you might want to try using the category below to correct that.
Here is a great reference in the Core Data Programming Guide.
This is one of those convenience methods that we developers must provide on our end. In your project, replace some_moc
with your managed object context.
@implementation NSManagedObjectID (FetchSource)
- (id) valueForUndefinedKey:(NSString *)key {
//Attempt to unwrap the underlying object from the moc
NSManagedObject *mocObject = [some_moc objectWithID:self];
return [object valueForKey:key];
}
@end
I hope that works for you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With