How to convert RealmSwift list to results?

I am using Realm List/Results as my data source for a UITableView . At some point, I will assign him a list. as:

 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 has changed the filter dates, I like this:

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

But the above line throws an error because the return type filter is a Results object, and aRealmObject.someList is a list.

What is the best way to handle this situation?

  • make dataSource as List and convert Results object to List ? How??
  • do dataSource as Results and convert List to Results ? How??
  • Or maybe you have a better way to do this, please share it with me.

Thanks,

+6
source share
2 answers

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

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

I found an easy way to convert List to Results using the filter method, it always returns a Results object. Just gave him a true predicate.

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

All Articles