问题
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
- Separating Query results and Relationships in two separate classes
- 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