Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

ios - RLMArray - retrieve Array from Object

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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...