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