Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 943
Dmitry Nelepov Avatar asked Sep 07 '16 01:09

Dmitry Nelepov


People also ask

What is fetched property in Core Data?

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.

What is relationship in Core Data?

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.


1 Answers

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!

like image 172
Sheamus Avatar answered Oct 19 '22 09:10

Sheamus