How to convert RealmResults object to RealmList?
RealmList <Student> results = new RealmList<Student>();
results.addAll(realmResultList.subList(0, realmResultList.size()));
RealmSwift: Convert Results to Swift Array
I found a solution. Created extension on Results.
extension Results {
func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for i in 0 ..< count {
if let result = self[i] as? T {
array.append(result)
}
}
return array
}
}
and using like
class func getSomeObject() -> [SomeObject]? {
let objects = Realm().objects(SomeObject).toArray(SomeObject) as [SomeObject]
return objects.count > 0 ? objects : nil
}
how to convert RealmListT to RealmResultsT
Simple use query on your list :
class MyObject extends RealmObject {
RealmList<MySubObject> subObjects;
}
...
myObject.subObjects.where().findAll(); //returns a RealmResults
//you can query your list like
myObject.subObjects
.where()
.equalTo("status", "done")
.findAll(); //you can use sort, findFirst etc
how to convert RealmSwift List to Results?
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)
Convert Swift Array of Realm Objects Back to Realm Results
Do this:
let entryObjects = realm.objects(Entry.self).filter(...)
let total = entryObjects.map({ $0.fieldValues.sum(ofProperty: "total") }).reduce(0, +)
When Realm support aggregate operations on nested keypaths (#3392) it'll be possible to use KVC for this, but that's still in progress.
How to convert an array to List in Realm?
Note that your problem is not about converting List
to array, but rather converting an array to List
. You already know how to do the former: Array(someList)
, but since you are assigning an array (movie.genreIDs ?? []
) to a List
property (movieRealm.genreIDs
), you need to convert an array to a List
.
But wait! movieRealm.genreIDs
is a let
constant, so you can't assign to it anyway. Your real problem is that you haven't declared the genreIDs
property correctly.
If you follow the docs on how to declare a property of a List
type:
class CachedMovies: Object {
// genreIDs should not be optional
let genreIDs = List<Int>()
}
You'll see that you don't need to convert arrays to List
. You can just add the contents of the array to the List
, since the list is not nil:
movieRealm.genreIDs.append(objectsIn: move.genreIDs ?? [])
Note that if you actually want an optional list property (a property which can be nil, empty, or have elements), Realm doesn't support that.
Realm: ResultsT als ListT
We can use extensions to make life easier :
extension Realm {
func list<T: Object>(_ type: T.Type) -> List<T> {
let objects = self.objects(type)
let list = objects.reduce(List<T>()) { list, element -> List<T> in
list.append(element)
return list
}
return list
}
}
Usage:
let objects = realm.list(YourObject.self)
How to convert RealmResultsObject to ListObject
To eagerly read every element from the Realm (and therefore make all elements in the list become unmanaged, you can do):
List<StepEntry> arrayListOfUnmanagedObjects = realm.copyFromRealm(realmResults);
But you generally have absolutely no reason to do that unless you want to serialize the objects with GSON (specifically, because it reads field data with reflection rather than with getters), because Realm was designed in such a way that the list exposes a change listener, allowing you to keep your UI up to date just by observing changes made to the database.
Related Topics
Swift How to Make List Start at The Top with No Padding
Can't Dismiss View Controller That's Embedded in a Navigation Controller
How to Replace My .Xib File with Pure Swift 3
Decoding Different Type with and Without Array
Why Cannot Read The Receipt Data for On-Device Validation
Crashlytics Doesn't Work with Xcode 10 Beta
How to Grab The Parent Object from a Subview
How to Use Nscalendar Range Function Within Calendar
How to Dynamically Hide Navigation Back Button in Swiftui
Can't Set @Ibinspectable Computed Property in UIview
Uitextfield with a Placeholder Containing an Image and Text
Using Nsdate to Get Date for Easter
Print Not Working in Swift 3 Extensions
Add a File Generated by Run Script into The Test Target Compilation List in Xcode
No Trailing Closures Support for Methods with Default Parameter Values