JDG
JDG

Reputation: 540

How to get all objects of a class in Realm to an array

Currently I am using this:

    var matchedUsersFromRealm = MatchedUser.allObjects()
    var matchedUsersInRealm = RLMArray(objectClassName: MatchedUser.className())
    matchedUsersInRealm.removeAllObjects()
    matchedUsersInRealm.addObjects(matchedUsersFromRealm)

But it just looks cumbersome rather than just getting it in one line as it should (or did?). Maybe there is a better way?

PS, I am working on a mix project and somehow I can only use the Objective-C version and bridge it to my swift project. So the Realm().objects() is not available, even though it returns a Results not an array.

Upvotes: 12

Views: 26718

Answers (6)

Peter Lapisu
Peter Lapisu

Reputation: 20985

just do

let array = Array(realmResult)

Upvotes: 1

Rikesh Subedi
Rikesh Subedi

Reputation: 1855

I preferred to add a helper class to save and retrieve any type of objects using Generics.

class RealmHelper {
    static func saveObject<T:Object>(object: T) {
        let realm = try! Realm()
        try! realm.write {
            realm.add(object)
        }
    }
    static func getObjects<T:Object>()->[T] {
        let realm = try! Realm()
        let realmResults = realm.objects(T.self)
        return Array(realmResults)

    }
    static func getObjects<T:Object>(filter:String)->[T] {
        let realm = try! Realm()
        let realmResults = realm.objects(T.self).filter(filter)
        return Array(realmResults)

    }
}

Upvotes: 8

ixx
ixx

Reputation: 32273

You can add these extensions:

import Foundation
import RealmSwift

extension Results {

    func toArray() -> [T] {
        return self.map{$0}
    }
}

extension RealmSwift.List {

    func toArray() -> [T] {
        return self.map{$0}
    }
}

And then when fetching:

do {
    let realm = try Realm()
    let objs = realm.objects(MyObjType).toArray() 
    // ...

} catch _ {
    // ...
}

(Remove do try catch if you're using Swift pre-2.0)

Note that this loads everything into memory at once, which may be in some cases not desired. If you're fetching in the background, it's required though, as Realm currently doesn't support using the objects in the main thread after that (you will also have to map the array to non-Realm objects in this case).

Upvotes: 26

segiddins
segiddins

Reputation: 4120

RLMArrays are meant to represent to-many relationships. RLMResults are meant to represent the result of a database query. If, however, you're looking to get a plain old Swift array (and are OK with performance tradeoffs of using that versus an RLMResults), you can do map(MatchedUser.allObjects()) { $0 }

Upvotes: 1

Huy Le
Huy Le

Reputation: 2513

Because RLMResults is a lazy loading array. So if you want to return an array, just write an extension for Results. Example:

extension Result {
  func toArray() -> [T] {
    let newArray: [T] = []
    // At here you should append the value from self into newArray
    ...
    return newArray
  }
}

Upvotes: 0

jimmy0251
jimmy0251

Reputation: 16463

You can get all objects of User in 1 line :

let matchedUsers = Realm().objects(MatchedUser)

Upvotes: 1

Related Questions