Ios 领域模型中的递归关系
如果我希望具有相同用户模型的领域数组,则会发生异常。Ios 领域模型中的递归关系,ios,objective-c,recursion,realm,relationships,Ios,Objective C,Recursion,Realm,Relationships,如果我希望具有相同用户模型的领域数组,则会发生异常。 RLMException(@“RLMArray属性需要定义包含类型的协议-例如:RLMArray”) 那么有解决办法吗?如何在如下领域中实现递归关系 #import <Realm/Realm.h> @interface User : RLMObject @property NSInteger userId; @property NSString *displayName; @property RLMArray<User>
RLMException(@“RLMArray属性需要定义包含类型的协议-例如:RLMArray”)代码>
那么有解决办法吗?如何在如下领域中实现递归关系
#import <Realm/Realm.h>
@interface User : RLMObject
@property NSInteger userId;
@property NSString *displayName;
@property RLMArray<User> *friends;
- (instancetype)initWithDictionary:(NSDictionary *)data;
@end
RLM_ARRAY_TYPE (User)
#导入
@界面用户:RLMObject
@属性NSInteger userId;
@属性NSString*displayName;
@玛丽*朋友酒店;
-(instancetype)initWithDictionary:(NSDictionary*)数据;
@结束
RLM_数组_类型(用户)
我认为术语是“反向关系”,我从未尝试使用同一类的嵌套对象引用和对象。
但在Realm纪录片中,他们有一个和“狗”和“主人”的例子。
主人可以养狗,狗也可以养主人,它们有一种“反向关系”
应该是这样的:
@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@property (readonly) RLMLinkingObjects *owners;
@end
@implementation Dog
+ (NSDictionary *)linkingObjectsProperties {
return @{
@"owners": [RLMPropertyDescriptor descriptorWithClass:Person.class propertyName:@"dogs"],
};
}
@end
ref:如例外情况所述,您需要声明一个协议来定义RLMArray
的包含类型。这就是RLM\u ARRAY\u TYPE
宏的作用。这里的特殊之处在于,您需要将此声明放在接口声明之前,这可以通过使用@class
预先声明用户
类型来完成。您可以这样做:
#import <Realm/Realm.h>
@class User;
RLM_ARRAY_TYPE (User)
@interface User : RLMObject
@property NSInteger userId;
@property NSString *displayName;
@property RLMArray<User> *friends;
- (instancetype)initWithDictionary:(NSDictionary *)data;
@end
#导入
@类用户;
RLM_数组_类型(用户)
@界面用户:RLMObject
@属性NSInteger userId;
@属性NSString*displayName;
@玛丽*朋友酒店;
-(instancetype)initWithDictionary:(NSDictionary*)数据;
@结束
realm文档说我们应该在模型界面底部的末尾添加宏。是的,除了在顶部添加宏之外,向前声明也是解决方案。谢谢。我认为这个解决方案也应该基于本文中更清晰的示例