RLMLinkingObjects循环依赖

时间:2016-05-01 12:51:06

标签: dependencies realm circular-dependency

我遇到了循环依赖的问题:当使用新的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)

1 个答案:

答案 0 :(得分:5)

这是由于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代码都应位于同一个文件中。

相关问题