how to convert RealmSwift List to Results?

雨燕双飞 提交于 2019-12-22 04:26:23

问题


I am using a Realm List/Results as my dataSource for a UITableView. At some point I assign a list to it. like:

var dataSource:List<SomeObject>! // Or >> Results<SomeObject>!
let aRealmObject =  realm.objectForPrimaryKey(SomeObject.self, key: objectId) 
dataSource = aRealmObject.someList // dataSource should be List

Then I have a filter on this list If the user changed the filter dates, I do like this:

dataSource = dataSource.filter("FILTER THE DATES",newDates) // dataSource should be Results

But the line above causes an error as the return type of filter is a Results object and aRealmObject.someList is a List.

What is the best way to deal with this situation?

  • make dataSource as a List and convert the Results object to List? How??
  • make dataSource as a Results and convert the List to Results? How??
  • Or may be you have a better way of doing it, Please share it with me.

Thanks,


回答1:


Both List and Results (as well as LinkingObjects) can be converted into an AnyRealmCollection type. I think this is probably the best way to standardize all of Realm's array-type types:

var dataSource:AnyRealmCollection!
let aRealmObject = realm.objectForPrimaryKey(SomeObject.self, key: objectId) 
dataSource = AnyRealmCollection(aRealmObject.someList)



回答2:


I have found A simple way to convert List to Results making use if the filter method, it always returns Results object. Just gave it a true predicate.

   dataSource = aRealmObject.someList.filter("TRUEPREDICATE") //this is a Results object.


来源:https://stackoverflow.com/questions/37184174/how-to-convert-realmswift-list-to-results

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