Realm-cocoa: Many to Many realtions problem

Created on 11 Aug 2017  路  5Comments  路  Source: realm/realm-cocoa

Im trying to create two Realm Objects: UserObject and GroupObject. The problem is that i have a many to many realtionship between them, where the user has n groups and each group has n users. First I got stuck into the interdependency problem, which one has the answer here First Answer and here Second Answer.

I followed the instructions and stored all interfaces and implementations inside the same file, but now I'm having the following error:

'RLMException', reason: 'Property 'groups' requires a protocol defining the contained type - example: RLMArray.'

Some of my Realm Objects code:

#import <Realm/Realm.h>
@class HSGroup;
@class HSUser;

RLM_ARRAY_TYPE(GroupObject);
RLM_ARRAY_TYPE(UserObject);

@interface GroupObject : RLMObject
@end

@interface UserObject : RLMObject
@end

@interface GroupObject ()

@property NSString *groupId;
@property NSNumber<RLMDouble> *latitude;
@property NSNumber<RLMDouble> *longitute;
@property RLMArray<UserObject *> *members;
@property NSString *groupName;
@property NSString *imageURL;
@property NSDate *modifiedAt;

-(instancetype)initWithGroup:(HSGroup *)group;

@end

@interface UserObject ()

@property NSString *userIdentifier;
@property NSString *name;
@property NSNumber<RLMDouble> *actualLatitude;
@property NSNumber<RLMDouble> *actualLongitute;
@property RLMArray<GroupObject *> *groups;
@property NSString *imageURL;
@property NSDate *modifiedAt;

-(instancetype)initWithUser:(HSUser *)user;

@end

Realm framework version: latest

Xcode version: 8.3.3

iOS/OSX version: 10.12.6

T-Help

Most helpful comment

@property RLMArray<UserObject *> *members;

should be

@property RLMArray<UserObject><UserObject *> *members;

And similarly for your other RLMArray properties.

The former declares an RLMArray that is constrained via Objective-C lightweight generics to contain only UserObjects. Objective-C generics are type-erased, meaning that information about their types is not available at runtime. The latter declares an RLMArray that both uses Objective-C generics and conforms to a UserObject protocol (the protocol is created by the RLM_ARRAY_TYPE macro). Information about protocol conformance _is_ available at runtime, and is what Realm Objective-C looks at to determine what object type your array property contains.

All 5 comments

@property RLMArray<UserObject *> *members;

should be

@property RLMArray<UserObject><UserObject *> *members;

And similarly for your other RLMArray properties.

The former declares an RLMArray that is constrained via Objective-C lightweight generics to contain only UserObjects. Objective-C generics are type-erased, meaning that information about their types is not available at runtime. The latter declares an RLMArray that both uses Objective-C generics and conforms to a UserObject protocol (the protocol is created by the RLM_ARRAY_TYPE macro). Information about protocol conformance _is_ available at runtime, and is what Realm Objective-C looks at to determine what object type your array property contains.

Nice explanation. But what about Protocol qualifiers must precede type arguments error? Do you know how to solve it?

Sorry, I typed that on my phone and neglected to double-check the order of the protocol vs generic constraint. It should be reversed from what I said:

@property RLMArray<UserObject *><UserObject> *members;

That worked perfectly. Thank you 馃槃

Glad to hear it!

Was this page helpful?
0 / 5 - 0 ratings