How to make RLMResults mutable?

会有一股神秘感。 提交于 2019-12-10 14:12:19

问题


The Realm doc says the RLMResults are lick NSArray. I have some results returned from the database and I want to merge it into another RLMResults. But it seems it's immutable, How to make a RLMResults add objects from another RLMResults? or make it mutable? or convert it to NSArray?


回答1:


Currently this is something you would have to do manually. You could create an RLMArray by concatenating your two results.

We are discussing a union/merge method further down on the roadmap for RLMObjects of the same type though.

Any bit you can share will help us understand the use cases and potentially impact the api design

As long as they are the same type, here's a generic example

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)



回答2:


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

Basically I convert the 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 }

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



来源:https://stackoverflow.com/questions/28847359/how-to-make-rlmresults-mutable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!