RLMArray properties in unmanaged RLMObjects in Objective-C

落花浮王杯 提交于 2019-12-12 04:45:16

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/43641072/rlmarray-properties-in-unmanaged-rlmobjects-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!