Marcel
Marcel

Reputation: 123

Convert RLMResults to RLMArray

I am querying over a RLMArray with objectsWhere and i get a RLMResults, but i need a RLMArray with the results a this point in my code.

private var data: RLMArray?

self.data = self.currentSubcategory!.datasheets // is a RLMArray
self.data = self.data!.objectsWhere("is_favourite = 1")

Upvotes: 12

Views: 9369

Answers (3)

Peter Lapisu
Peter Lapisu

Reputation: 21005

You need to add objects from RLMResult to RLMArray...

@implementation RLMResults (RLMArrayConversion)

- (RLMArray *)toArray {
    RLMArray * array = [[RLMArray alloc] initWithObjectClassName:self.objectClassName];
    [array addObjects:self];
    return array;
}

@end

Upvotes: 6

Vi Matviichuk
Vi Matviichuk

Reputation: 1132

RLMArray has been split into two classes: RLMArray and RLMResults. RLMArray is now used only for to-many properties on RLMObject classes, while RLMResults is used for all of the querying and sorting methods. This was done to reflect that the two actually had fairly different APIs (for example, RLMResults does not have addObject:), and they’re expected to diverge further as we add change notifications for queries.

The migration for this should be as simple as replacing RLMArray with RLMResults in all of the places that the compiler complains about.

To go with this, arraySortedByProperty:ascending: has been renamed to sortedResultsUsingProperty:ascending:, and addObjectsFromArray: has been renamed to addObjects: to reflect the fact that you can pass any enumerable object to it (such as NSArray, RLMArray, or RLMResults).

Source: http://realm.io/news/realm-cocoa-0.87.0/#rlmresults

Hope that's enough

Upvotes: 12

Thomas Goyne
Thomas Goyne

Reputation: 8138

let datasheets = self.currentSubcategory!.datasheets!.objectsWhere("is_favourite = 1")
let objects = Array(datasheets.generate())

self.data!.removeAllObjects()
self.data!.addObjects(objects)

Creating the array of the results is required because the self.data!.removeAllObjects() line will also clear datasheets, since RLMResults are live-updating as you make changes to the source.

Upvotes: 4

Related Questions