使用RealmSwift保存一对多关系对象
在关系方面,我来自一个类似RubyonRails的数据结构 所以在Rails中:Foo有很多条,Bar有一条Foo 通过查阅RealmSwift文档,我想到了这个,我想:使用RealmSwift保存一对多关系对象,swift,realm,swift4,swift4.2,Swift,Realm,Swift4,Swift4.2,在关系方面,我来自一个类似RubyonRails的数据结构 所以在Rails中:Foo有很多条,Bar有一条Foo 通过查阅RealmSwift文档,我想到了这个,我想: class Foo: Object { // other props var bars = List<Bar>() // I hope this is correct } class Bar: Object { // other props @objc dynamic var foo: Foo?
class Foo: Object {
// other props
var bars = List<Bar>() // I hope this is correct
}
class Bar: Object {
// other props
@objc dynamic var foo: Foo?
}
最后,我应该能够foo.bar
查看bar
数组,并bar.foo
获取foo
foo
和bar
尚未创建,因此不知道如何链接批次以立即保存。可能的怎么用?如果您提供的是答案,您是否可以发布文档参考以供将来参考?这对我来说算是一个答案。谢谢这应该让您开始:
class Foo: Object {
// other props
@objc dynamic var id = ""
let bars = List<Bar>()
override static func primaryKey() -> String? {
return "id"
}
}
class Bar: Object {
// other props
@objc dynamic var id = ""
let foo = LinkingObjects(fromType: Foo.self, property: "bars")
override static func primaryKey() -> String? {
return "id"
}
}
let foo = Foo()
foo.id = "somethingUnique"
foo.someProp = "Mike"
let bar = Bar()
bar.id = "somethingUnique"
bar.someProp1 = "some value 1"
try! realm.write {
realm.add(foo)
realm.add(bar)
foo.bars.append(bar)
}
let anotherBar = Bar()
anotherBar.id = "somethingUnique"
anotherBar.someProp1 = "some other value"
try! realm.write {
realm.add(anotherBar)
foo.bars.append(anotherBar)
}
如果我正确理解您的评论:
// already created foo
for nonRealmBar in nonRealmBars {
// Note: you could also use realm.create
let bar = Bar()
bar.id = nonRealmBar.id
bar.someProp = nonRealmBar.someProp
// fill in other properties;
try! realm.write {
realm.add(bar)
foo.bars.append(bar)
}
}
这应该让你开始:
class Foo: Object {
// other props
@objc dynamic var id = ""
let bars = List<Bar>()
override static func primaryKey() -> String? {
return "id"
}
}
class Bar: Object {
// other props
@objc dynamic var id = ""
let foo = LinkingObjects(fromType: Foo.self, property: "bars")
override static func primaryKey() -> String? {
return "id"
}
}
let foo = Foo()
foo.id = "somethingUnique"
foo.someProp = "Mike"
let bar = Bar()
bar.id = "somethingUnique"
bar.someProp1 = "some value 1"
try! realm.write {
realm.add(foo)
realm.add(bar)
foo.bars.append(bar)
}
let anotherBar = Bar()
anotherBar.id = "somethingUnique"
anotherBar.someProp1 = "some other value"
try! realm.write {
realm.add(anotherBar)
foo.bars.append(anotherBar)
}
如果我正确理解您的评论:
// already created foo
for nonRealmBar in nonRealmBars {
// Note: you could also use realm.create
let bar = Bar()
bar.id = nonRealmBar.id
bar.someProp = nonRealmBar.someProp
// fill in other properties;
try! realm.write {
realm.add(bar)
foo.bars.append(bar)
}
}
if let bar = realm.object(ofType: Bar.self, forPrimaryKey: "theUniqueID") {
if let foo = bar.foo.first {
// you have your foo
}
}
// already created foo
for nonRealmBar in nonRealmBars {
// Note: you could also use realm.create
let bar = Bar()
bar.id = nonRealmBar.id
bar.someProp = nonRealmBar.someProp
// fill in other properties;
try! realm.write {
realm.add(bar)
foo.bars.append(bar)
}
}