badeleux
badeleux

Reputation: 592

Inverse relation in realm

Let's assume two tables Boxand Item. Box may have many items, one item have only one box. I would like to fetch all items which have box is in given array. How could I do that? In CD I would do it by predicate and property in Item class which stands for connection to Box.

I am using version 0.81

Upvotes: 3

Views: 2688

Answers (1)

jpsim
jpsim

Reputation: 14409

UPDATE (10-27-2014)

Bidirectional relationships are now supported. See Realm's docs: http://realm.io/docs/cocoa/latest#inverse-relationships

ORIGINAL ANSWER

Bidirectional relationships must be explicitly linked at this time. Here's an example:

@class Box;

@interface Item : RLMObject
@property Box *box;
@end

RLM_ARRAY_TYPE(Item);

@interface Box : RLMObject
@property RLMArray<Item> *items;
@end

...

Item *item = [[Item alloc] init];
Box *box = [[Box alloc] initWithObject:@[@[item]]];
item.box = box;

We have plans to simplify this pattern in the future.

This answer was taken from GitHub

Upvotes: 6

Related Questions