如何在 Realm 中实现继承 (iOS, Objective C)
How to implement Inheritance in Realm (iOS, Objective C)
我有一个实体:
- 'Person' - 摘要 class。
- 'Contact' - 来自 Person
的子class
- 'User' - 来自 Person
的子class
- 'Activity' - 其中包含一个 relatedPerson 作为 Person 类型,但它可以是 Contact 或 User。
我的问题:当我缓存 'Activity' 时,Realm 将 relatedPerson 缓存为 'Person' 类型并丢失 'User' 或 'Contact' 的所有数据。如何解决这个问题?
我的classes结构是
@interface Person : RLMObject
@property NSString *uID;
- (NSString *)displayName;
...
@end
@interface Contact : Person
@property NSString *fullName;
...
@end
@interface User : Person
@property NSString *nickname;
...
@end
@interface Activity : RLMObject
@property NSString *uID;
@property NSDate *createdAt;
@property Person *relatedPerson;
@end
Realm 还不直接支持您所追求的类型的继承。正在 GitHub issue 1109. In the meantime, you can see a few different approaches to emulating inheritance in the comments of that issue by @jpsim and @mrackwitz 中跟踪对它的支持。哪种方法最适合您将取决于您的模型的要求。
我有一个实体:
- 'Person' - 摘要 class。
- 'Contact' - 来自 Person 的子class
- 'User' - 来自 Person 的子class
- 'Activity' - 其中包含一个 relatedPerson 作为 Person 类型,但它可以是 Contact 或 User。
我的问题:当我缓存 'Activity' 时,Realm 将 relatedPerson 缓存为 'Person' 类型并丢失 'User' 或 'Contact' 的所有数据。如何解决这个问题?
我的classes结构是
@interface Person : RLMObject
@property NSString *uID;
- (NSString *)displayName;
...
@end
@interface Contact : Person
@property NSString *fullName;
...
@end
@interface User : Person
@property NSString *nickname;
...
@end
@interface Activity : RLMObject
@property NSString *uID;
@property NSDate *createdAt;
@property Person *relatedPerson;
@end
Realm 还不直接支持您所追求的类型的继承。正在 GitHub issue 1109. In the meantime, you can see a few different approaches to emulating inheritance in the comments of that issue by @jpsim and @mrackwitz 中跟踪对它的支持。哪种方法最适合您将取决于您的模型的要求。