0

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];
Victor
  • 914
  • 12
  • 15

1 Answers1

6

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.

timanglade
  • 371
  • 1
  • 2
  • 7
  • 2
    thanks for the great explanation. P.S. Nevertheless, realm is easy to use and yet powerful DB. Best regards! – Victor Sep 18 '14 at 08:04
  • 1
    Just tried to call "objectsWhere" on relationship and ended up with the same exception. It would be nice to have this working. – zvonicek Oct 11 '14 at 09:54
  • I'm experiencing the same issue as zvonicek, only I'm trying to use ObjectsWithPredicate: – user3344977 Oct 14 '14 at 21:31
  • We’re actually working on this this week. There’s a few things already committed in our master branch on https://github.com/realm/realm-cocoa + more stuff in work-in-progress branches. We apologize for the confusion! – timanglade Oct 15 '14 at 21:34
  • Sinde 2014-10-21 they already splited `RLMArray` in two classes with separate concerns. Its the 0.87.0 release, check their [github](https://github.com/realm/realm-cocoa/blob/master/CHANGELOG.md#0870-release-notes-2014-10-21). – Orgmir Nov 18 '14 at 12:28