RLMArray - retrieve Array from Object

☆樱花仙子☆ 提交于 2019-11-28 06:45:48

问题


My model:

Conv.h

#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)

When I'm trying to extract ConvTexts from Conv:

Conv *c = [Conv objectsWhere:@"c_id = %@",@(1)];
ConvText *ct = [c.cts arraySortedByProperty:@"time" ascending:NO][0]; <--

I get this message: 'RLMException', reason: 'This method can only be called in RLMArray instances retrieved from an RLMRealm'

I also try like this:

RLMArray *cts = c.cts;
ConvText *ct = [cts arraySortedByProperty:@"time" ascending:NO][0];

回答1:


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

  1. Separating Query results and Relationships in two separate classes
  2. Allowing (most) Query methods to be called on Relationships.

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.



来源:https://stackoverflow.com/questions/25888339/rlmarray-retrieve-array-from-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!