user2363025
user2363025

Reputation: 6505

realm commit write error - Can't commit a non-existing write transaction

I am trying to add a record to a realm DB table.

I have a class Connection which represents a table I need in my DB and have created dynamic vars which are to represent the columns:

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")
    }
}

Then in my code I am trying to write and commit the write transaction like so:

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

try! self.realm.commitWrite()

When running this code, I am receiving the error:

Can't commit a non-existing write transaction

What am I missing? Do I need to have inits in my ConnectionState class? Before adding in the commitWrite, I was trying to view the db with realm browser. I found my device in xCode and chose to download the container but it was empty. Then I thought I needed to add in commitWrite

Upvotes: 3

Views: 3742

Answers (2)

Yannick
Yannick

Reputation: 3278

In your example you called commitWrite without having called beginWrite. You cannot commit a write transaction because you did not start one. Either start a write transaction or delete the commitWrite line.

  1. Start transaction and commit it

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

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

The Realm docs have two examples of adding data to the database.

  1. Use the realm.write method

    // 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. Use realm.beginWrite() and realm.commitWrite() to start the write transaction and commit data to the database

    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()
    }
    

Upvotes: 4

EpicPandaForce
EpicPandaForce

Reputation: 81539

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

This code is somewhat equivalent to (possibly with some additional error handling):

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

Which means you're trying to commit your writes twice.

Just change your code like this:

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

// try! self.realm.commitWrite()

Upvotes: 4

Related Questions