如何将Realm中类的所有对象都添加到数组中

时间:2022-09-24 23:13:02

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.

PS,我正在研究混合项目,不知怎的,我只能使用Objective-C版本并将其桥接到我的快速项目中。所以Realm()。objects()不可用,即使它返回的结果不是数组。

5 个解决方案

#1


19  

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)

(如果你使用的是Swift pre-2.0,请删除do try catch)

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).

请注意,这会立即将所有内容加载到内存中,在某些情况下可能不需要。如果您在后台获取,则需要它,因为Realm当前不支持在主线程中使用对象(在这种情况下,您还必须将数组映射到非Realm对象)。

#2


1  

You can get all objects of User in 1 line :

您可以在一行中获取User的所有对象:

let matchedUsers = Realm().objects(MatchedUser)

#3


1  

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 }

RLMArrays旨在表示多对多关系。 RLMResults用于表示数据库查询的结果。但是,如果你想获得一个普通的旧Swift数组(并且可以使用它与RLMResults进行性能权衡),你可以做map(MatchedUser.allObjects()){$ 0}

#4


0  

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

因为RLMResults是一个延迟加载数组。因此,如果要返回数组,只需为结果编写扩展名。例:

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

#5


0  

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)

    }
}

#1


19  

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)

(如果你使用的是Swift pre-2.0,请删除do try catch)

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).

请注意,这会立即将所有内容加载到内存中,在某些情况下可能不需要。如果您在后台获取,则需要它,因为Realm当前不支持在主线程中使用对象(在这种情况下,您还必须将数组映射到非Realm对象)。

#2


1  

You can get all objects of User in 1 line :

您可以在一行中获取User的所有对象:

let matchedUsers = Realm().objects(MatchedUser)

#3


1  

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 }

RLMArrays旨在表示多对多关系。 RLMResults用于表示数据库查询的结果。但是,如果你想获得一个普通的旧Swift数组(并且可以使用它与RLMResults进行性能权衡),你可以做map(MatchedUser.allObjects()){$ 0}

#4


0  

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

因为RLMResults是一个延迟加载数组。因此,如果要返回数组,只需为结果编写扩展名。例:

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

#5


0  

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)

    }
}