Darren Ehlers
Darren Ehlers

Reputation: 633

RLMArray properties in unmanaged RLMObjects in Objective-C

I cannot find a good example code of this anywhere....but the information I find is contradictory and confusing...

@interface DAORealmMetadata : RLMObject
@property (nonatomic, copy) NSString*     id;
@end
RLM_ARRAY_TYPE(DAORealmMetadata)

@interface DAORealmBase : RLMObject
@property (nonatomic, copy) NSString*     id;
@property (nonatomic, copy)  RLMArray<DAORealmMetadata*><DAORealmMetadata>*      metadata;
@end
RLM_ARRAY_TYPE(DAORealmBase)

Question: Am I supposed to add @dynamic metadata in the DAORealmBase implementation...or not?

I've tried it with and without and have the same end result...a crash.

I create the unmanaged object with this code:

DAORealmBase* baseObj = [[DAORealmBase alloc] init];

DAORealmMetadata* metadataObj = [[DAORealmMetadata alloc] init];

[baseObj.metadata addObject:metadataObj];

Question: Why does the last line cause a crash/exception?

I can only assume that I"m doing something wrong, but I cannot find any specifics as to what I did.

Thanks!

Upvotes: 0

Views: 333

Answers (1)

Darren Ehlers
Darren Ehlers

Reputation: 633

Well, I tracked the problem down, and through some trial and error, determined that the problem was the property attributes on the RLMArray properties.

Changing

@property (nonatomic, copy) RLMArray<DAORealmMetadata*><DAORealmMetadata>* metadata;

to

@property RLMArray<DAORealmMetadata*><DAORealmMetadata>* metadata;

seems to have resolved the problem. I believe specifically the 'copy' attribute.

Now, I know that the Realm docs say that the attributes are ignored and not needed, but the lint checker I'm using wants them there...and since they are "ignored", what's the harm?

Well, they are ignored on normal Realm properties, but on the RLMArray properties they aren't ignored, and problems ensue.

Hopefully this will help someone else in the future and save them some time.

Upvotes: 1

Related Questions