Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert RealmSwift List to Results?

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,

like image 815
Ismail Avatar asked May 12 '16 10:05

Ismail


2 Answers

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)
like image 33
catalandres Avatar answered Nov 12 '22 07:11

catalandres


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.
like image 159
Ismail Avatar answered Nov 12 '22 07:11

Ismail