连接 Realm 和 SwiftBond 的最佳方式是什么 [英] What is the best way to connect Realm and SwiftBond
问题描述
我喜欢 Realm 并且我喜欢 债券.它们都使应用程序创建成为一种乐趣.所以我想知道连接 Realm 和 Bond 的最佳方式是什么?
在 Realm 中,我们可以存储基本类型,例如 Int
、String
,例如但是在 Bond 中,我们使用 Dynamic
s 和 Bond
s.我发现连接 Realm 和 Bond 的唯一方法如下:
I love Realm and I love Bond. Both of them makes app creation a joy. So I was wondering what is the best way to connect Realm and Bond?
In Realm we can store basic types such as Int
, String
, e.g. But in Bond we work with Dynamic
s and Bond
s. The only way that I found to connect Realm and Bond is following:
class TestObject: RLMObject {
dynamic var rlmTitle: String = ""
dynamic var rlmSubtitle: String = ""
var title: Dynamic<String>
var subtitle: Dynamic<String>
private let titleBond: Bond<String>!
private let subtitleBond: Bond<String>!
init(title: String, subtitle: String) {
self.title = Dynamic<String>(title)
self.subtitle = Dynamic<String>(subtitle)
super.init()
self.titleBond = Bond<String>() { [unowned self] title in self.rlmTitle = title }
self.subtitleBond = Bond<String>() { [unowned self] subtitle in self.rlmSubtitle = subtitle }
self.title ->> titleBond
self.subtitle ->> subtitleBond
}
}
但它确实缺乏简单和优雅,并产生了大量的锅炉代码.有什么办法可以做得更好吗?
But it surely lacks simplicity and elegance and produces a lot of boiler code. Is there any way to do this better?
推荐答案
我已经考虑了三天,并想出了近乎完美的解决方案,它不使用任何样板代码.首先,我为领域模型的包装器创建了一个超类:
I've been thinking about this for three days and came up with nearly perfect solution, which does not employ any boilerplate code. First of all I have created a super class for a realm model's wrapper:
class BondRealmBaseClass {
private var realmModel: RLMObject!
private let realm = RLMRealm.defaultRealm()
private var bonds = NSMutableArray()
init(){
realmModel = createRealmModel()
realm.beginWriteTransaction()
realm.addObject(realmModel)
realm.commitWriteTransaction()
createBonds()
}
init(realmModel: RLMObject){
self.realmModel = realmModel
createBonds()
}
func createBondFrom<T>(from: Dynamic<T>, toModelKeyPath keyPath: String){
from.value = realmModel.valueForKeyPath(keyPath) as T
let bond = Bond<T>() { [unowned self] value in
self.realm.beginWriteTransaction()
self.realmModel.setValue(value as NSObject, forKey: keyPath)
self.realm.commitWriteTransaction()
}
from ->| bond
bonds.addObject(bond)
}
//MARK: - Should be overriden by super classes
func createBonds(){ fatalError("should be implemented in supreclass") }
func createRealmModel() -> RLMObject{ fatalError("should be implemented in supreclass") }
}
之后我为每个领域模型创建两个类,第一个是实际的领域模型,它存储所有属性:
After that for each realm model I create two classes, first is the actual realm model, which stores all properties:
class RealmTodoModel: RLMObject {
dynamic var title = ""
dynamic var date = NSDate()
}
第二个是领域模型的包装器:
and a second one is the wrapper around realm model:
class TodoModel : BondRealmBaseClass{
let title = Dynamic("")
let date = Dynamic(NSDate())
override func createBonds(){
createBondFrom(title, toModelKeyPath: "title")
createBondFrom(date, toModelKeyPath: "date")
}
override func createRealmModel() -> RLMObject { return RealmTodoModel() }
}
这两个类实际上都是链接Realm
和Bond
所需要的:创建新的TodoModel
实际上会添加到Realm 新的>RealmTodoModel
以及对 TodoModel
的 title
和 date
所做的所有更改将自动保存到相应的 Realm代码>模型!
And this two classes is actually all is needed to link Realm
and Bond
: creating new TodoModel
will actually add to Realm new RealmTodoModel
and all changes made with TodoModel
's title
and date
will be automatically saved to corresponding Realm
model!
我添加了一些功能并将其作为框架发布在 GitHub 上.这是链接.
I added some functionality and posted this as a framework on GitHub. Here is the link.
这篇关于连接 Realm 和 SwiftBond 的最佳方式是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!