领域提交写入错误 - 无法提交不存在的写入事务

时间:2017-01-11 10:22:56

标签: swift database swift3 realm realm-mobile-platform

我正在尝试将记录添加到领域数据库表中。

我有一个类Connection,它代表我在数据库中需要的表,并创建了代表列的动态变量:

import Foundation
import RealmSwift
import Realm

open class ConnectionState: Object {

    open dynamic var _id : String = NSUUID().uuidString
    open dynamic var a : String = ""
    open dynamic var b : String = ""
    open dynamic var c : Int = 0

    open override class func primaryKey() -> String? {
        return "_id"
    }

    required public init() {
        super.init()
    }

    required public init(realm: RLMRealm, schema: RLMObjectSchema) {
        super.init(realm: realm, schema: schema)
    }

    required public init(value: Any, schema: RLMSchema) {
        fatalError("init(value:schema:) has not been implemented")
    }
}

然后在我的代码中,我尝试编写并提交写事务,如下所示:

let ConnectionState = ConnectionState()
ConnectionState.a = "a"
ConnectionState.b = "b"
ConnectionState.c = 1
try! self.realm.write {
     self.realm.add(ConnectionState)
}

try! self.realm.commitWrite()

运行此代码时,我收到错误:

Can't commit a non-existing write transaction

我错过了什么?我需要在ConnectionState类中使用inits吗? 在添加commitWrite之前,我试图用realm浏览器查看数据库。我在xCode中找到了我的设备,并选择下载容器,但它是空的。然后我想我需要添加commitWrite

2 个答案:

答案 0 :(得分:4)

try! self.realm.write {
     self.realm.add(ConnectionState)
}

这段代码有点等同于(可能还有一些额外的错误处理):

realm.beginWrite()
...
try! realm.commitWrite()

这意味着您要尝试两次提交。

只需更改您的代码:

try! self.realm.write {
     self.realm.add(ConnectionState)
}

// try! self.realm.commitWrite()

答案 1 :(得分:4)

在您的示例中,您调用了commitWrite而未调用beginWrite。您无法提交写入事务,因为您没有启动它。启动写入事务或删除commitWrite行。

  1. 启动事务并提交

    self.realm.beginWrite()
    
    self.realm.add(ConnectionState)
    
    try! self.realm.commitWrite()
    
  2. 删除commitWrite

    try! self.realm.write {
         self.realm.add(ConnectionState)
    }
    
  3. Realm docs有两个向数据库添加数据的例子。

    1. 使用realm.write方法

      // Use them like regular Swift objects
      let myDog = Dog()
      myDog.name = "Rex"
      myDog.age = 1
      print("name of dog: \(myDog.name)")
      
      // Get the default Realm
      let realm = try! Realm()
      
      // Query Realm for all dogs less than 2 years old
      let puppies = realm.objects(Dog.self).filter("age < 2")
      puppies.count // => 0 because no dogs have been added to the Realm yet
      
      // Persist your data easily
      try! realm.write {
          realm.add(myDog)
      }
      
    2. 使用realm.beginWrite()realm.commitWrite()启动写入事务并将数据提交到数据库

      let realm = try! Realm()
      
      // Break up the writing blocks into smaller portions
      // by starting a new transaction
      for idx1 in 0..<1000 {
        realm.beginWrite()
      
        // Add row via dictionary. Property order is ignored.
        for idx2 in 0..<1000 {
          realm.create(Person.self, value: [
            "name": "\(idx1)",
            "birthdate": Date(timeIntervalSince1970: TimeInterval(idx2))
          ])
        }
      
        // Commit the write transaction
        // to make this data available to other threads
        try! realm.commitWrite()
      }