Thursday, September 15, 2016

How to correct predicate CoreData Fetched property for Chat and Messages weak relationship?

Leave a Comment

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 

enter image description here

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?

1 Answers

Answers 1

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 Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment