问题描述
我的模特:
转化次数
#import <Realm/Realm.h>
#import "ConvText.h"
@interface Conv : RLMObject
@property NSInteger c_id;
@property RLMArray<ConvText> *cts;
@end
ConvText.h
#import <Realm/Realm.h>
@interface ConvText : RLMObject
@property NSInteger ct_id;
@property NSInteger time;
@end
RLM_ARRAY_TYPE(ConvText)
当我尝试从 Conv 中提取 ConvTexts 时:
Conv *c = [Conv objectsWhere:@"c_id = %@",@(1)];
ConvText *ct = [c.cts arraySortedByProperty:@"time" ascending:NO][0]; <--
我收到此消息:'RLMException',原因:此方法只能在从RLMRealm检索的RLMArray实例中调用"
我也尝试这样:
RLMArray *cts = c.cts;
ConvText *ct = [cts arraySortedByProperty:@"time" ascending:NO][0];
推荐答案
您会收到此错误,因为在后台查询结果和关系是两种不同类型的实体,即使它们通过同一类(RLMArray)公开也是如此.在这种情况下,您要在Relationship上调用Query方法(arraySortedByProperty),该方法仅在Query结果上可用,尽管我们当然也应该考虑将其添加到Relationships中!
You are getting this error because behind the scenes Query results and Relationships are two different types of entities, even though they are exposed through the same class (RLMArray). In this case, you are calling a Query method (arraySortedByProperty) on a Relationship, and that method is only available on Query results, although we sure should consider adding it to Relationships as well!
我们计划通过以下方式解决此问题
We plan on fixing this by
- 在两个单独的类中分隔查询结果和关系
- 允许在关系上调用(大多数)查询方法.
同时,很不幸,您必须将RLMArray复制到NSArray并进行排序:(我们知道它很烂,但我们只是获得了在C ++级别上对关系进行重新排序的支持,因此我们将其修复为下一个版本(0.86)
In the meantime, you unfortunately have to deep-copy the RLMArray into an NSArray and sort it :( We know it sucks but we just got support to re-sort Relationships at the C++ level so we’ll have that fixed in the next release (0.86)
我们的错误消息也应该更加明确-我们将尽快对其进行修复.
Our error message should be a lot more explicit as well — we’ll fix it asap.
这篇关于RLMArray-从对象检索数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!