How to make RLMResults mutable?

The Realm domain reports that RLMResults licks NSArray . I have some results returned from a database and I want to combine them into another RLMResults . But this seems immutable, how do RLMResults add objects from another RLMResults ? or make it volatile? or convert it to NSArray ?

+5
source share
2 answers

This is currently what you will need to do manually. You can create an RLMArray by combining your two results.

We discuss the merge / merge method later in the roadmap for RLMO objects of the same type.

Any bit you can provide will help us understand use cases and potentially influence api design

While they are of the same type, a general example is given here.

 let currentTask = Task.objectsWhere("name = %@", "First task").firstObject() as Task let currentRecords = currentTask.records let arrayOfRecords = RLMArray(objectClassName: "Record") arrayOfRecords.addObjects(currentRecords) let futureTask = Task.objectsWhere("name = %@", "Future task").firstObject() as Task let futureRecords = futureTask.records arrayOfRecords.addObjects(futureRecords) 
+3
source

I found a solution from duemunk: https://github.com/realm/realm-cocoa/issues/1046

I basically convert RLMResults to [RLMObject]: func toArray<T>(ofType: T.Type) -> [T] { var array = [T]() for result in self { if let result = result as? T { array.append(result) } } return array } func toArray<T>(ofType: T.Type) -> [T] { var array = [T]() for result in self { if let result = result as? T { array.append(result) } } return array }

let tracks = Track.allObjects().toArray(Track.self) // tracks is of type [Track]

+1
source

Source: https://habr.com/ru/post/1214635/


All Articles