连接Realm和SwiftBond的最佳方法是什么 [英] What is the best way to connect Realm and SwiftBond

查看:93
本文介绍了连接Realm和SwiftBond的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我爱领域,我爱债券.两者都使创建应用程序感到高兴.所以我想知道连接Realm和Bond的最佳方法是什么?
在Realm中,我们可以存储基本类型,例如IntString,例如但是在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 Dynamics and Bonds. 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() }
}

这两个类实际上是链接RealmBond所需要的:创建新的TodoModel实际上将为Realm添加新的RealmTodoModel以及使用TodoModeltitledate将自动保存到相应的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆