gpt4 book ai didi

ios - Realm 返回具有空 RLMArray 作为属性的对象

转载 作者:行者123 更新时间:2023-11-29 01:51:14 27 4
gpt4 key购买 nike

我有类Country。它是 RLMObject 的一个简单子(monad)类。基本上,当我保存 Country 时,它们确实有 regions (RLMArray 类型的属性包含几个 Region 类型的对象。它们是 RLMObject 子类)但是当我获取它时来自 Realm 然后 regions 为零。

我用浏览器仔细检查了 Realm ,那些数据就在那里。也许我使用了错误的技术来获取这些数据?

此时我使用 realm-objc 因为我必须支持设备

国家/地区(27 行)

public class Country: RLMObject {
public dynamic var name = ""
public dynamic var desc = ""
public dynamic var id = 0
public var regions = RLMArray(objectClassName: Region.className())
override public static func primaryKey() -> String? {
return "id"
}

override init!() {
super.init()
}

public func configure(id:Int,json:JSON){
self.name = json["name"].stringValue
if let desc = json["desc"].string {
self.desc = desc
}
for (key:String, value:JSON) in json["regions"] {
let region = Region()
region.configure(key.toInt()!,json:value)
self.regions.addObject(region)

}
self.id = json["id"].intValue
}
}

CountryManager(36 行)

public class CountryManager: NSObject {

public func saveWines(json:JSON) {
var countriesForStore = [Country]()
let db = RLMRealm.defaultRealm()
for (key:String, subJson:JSON) in json{
let country = Country() //Serialize each country
country.configure(key.toInt()!,json:subJson)
countriesForStore.append(country)
}

db.beginWriteTransaction()
db.addOrUpdateObjectsFromArray(countriesForStore)
db.commitWriteTransaction()
}

public func regionsForCountryWithNameLike(name:String) -> RLMResults {
let db = RLMRealm.defaultRealm()
let countryPredicate = NSPredicate(format: "name CONTAINS[c] %@", name)
let countries = Country.objectsWithPredicate(countryPredicate)

var rawRegionNames = [String]()
for country in countries {
if let c = country as? Country {
for region in c.regions{
rawRegionNames.append((region as! Region).name)
}
}

}
let regionNames = "','".join(rawRegionNames)
let regionPredicate = NSPredicate(format: "name IN {'\(regionNames)'}")
let regions = Region.objectsInRealm(db, withPredicate: regionPredicate)
return regions
}
}

最佳答案

尝试将 public var regions 更改为 dynamic public var regions,因为 Realm 要求 RLMArray 属性是动态的。

关于ios - Realm 返回具有空 RLMArray 作为属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31387424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com