加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖项 – RLMLinkingObjects循环依赖项

发布时间:2020-12-14 04:54:19 所属栏目:百科 来源:网络整理
导读:我有循环依赖的问题:当使用新的RLMLinkingObjects反向关系时,我收到以下错误: Type argument 'RCon *' does not satisfy the bound ('RLMObject *') of type parameter 'RLMObjectType' 我有两个课程RCon和RSan. RCon有多个RSan引用,RSan由多个RCon引用,因
我有循环依赖的问题:当使用新的RLMLinkingObjects反向关系时,我收到以下错误:

Type argument 'RCon *' does not satisfy the bound ('RLMObject *') of type parameter 'RLMObjectType'

我有两个课程RCon和RSan. RCon有多个RSan引用,RSan由多个RCon引用,因此它是多对多关系.
以下是类的声明示例.

头等舱:

//  RSan.h

#import <Realm/Realm.h>
#import <UIKit/UIKit.h>

@class RCon;

@interface RSan : RLMObject
@property (readonly) RLMLinkingObjects<RCon*>* cons;
@end
RLM_ARRAY_TYPE(RSan)

另一类:

//  RCon.h

#import <Realm/Realm.h>
#import <UIKit/UIKit.h>
#import "RSan.h"

@interface RCon : RLMObject
@property RLMArray<RSan*><RSan>* sans;
@end
RLM_ARRAY_TYPE(RCon)

解决方法

这是由于Objective-C编译器的限制. RLMArray的泛型约束需要它们的元素应该是RLMObject的子类.但是Objective-C编译器无法从@class转发声明中识别它.

要解决这个问题,我认为唯一的方法是在同一个文件中声明两个@interface,然后使用类扩展声明它们的属性.如下:

#import <Realm/Realm.h>
#import <UIKit/UIKit.h>

@interface RCon : RLMObject
@end
RLM_ARRAY_TYPE(RCon)

@interface RSan : RLMObject
@end
RLM_ARRAY_TYPE(RSan)

@interface RCon()
@property RLMArray<RSan*><RSan>* sans;
@end

@interface RSan()
@property (readonly) RLMLinkingObjects<RCon*>* cons;
@end

注意:所有avobe代码应该在同一个文件中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读