charliework
charliework

Reputation: 33

RealmSwift is requiring Realm also

I am migrating a project to Swift 3 and have encountered some issues with RealmSwift (2.6.1) and Genome (3.2.0). I'm getting errors for Realm in Xcode that say I need these inits:

required convenience init(realm: RLMRealm, schema: RLMObjectSchema) {
    self.init(realm: realm, schema: schema)
}

required convenience init(value: Any, schema: RLMSchema) {
    self.init(value: value, schema: schema)
}

However, that requires importing Realm in addition to RealmSwift, and when my class is initialized it is trying to use an RLMRealm instead of a Realm. The warning says 'required' initializer 'init(realm:schema:)' must be provided by subclass of 'Object', but that required init uses RLMRealm not Realm Any suggestions?

I am using Genome as well which requires this init, which is why Realm is asking for initializers in the first place:

required convenience init(node: Node, in context: Context) throws {
    self.init()
}

So inits look like this all together:

class BaseModel : RealmSwift.Object, MappableBase, Identifiable {

required init() {
    super.init()
}

required convenience init(node: Node, in context: Context) throws {
    self.init()
}

required convenience init(realm: RLMRealm, schema: RLMObjectSchema) {
    self.init(realm: realm, schema: schema)
}

required convenience init(value: Any, schema: RLMSchema) {
    self.init(value: value, schema: schema)
}

Everything worked fine in Swift 2.3 (using the corresponding Swift 2.3 versions of Realm and Genome) without any of these initializers, but now it is not working.

Entire Model:

import RealmSwift
import Genome
import Realm

protocol Identifiable {
    var identifier: String { get set }
}


class BaseModel : RealmSwift.Object, MappableBase, Identifiable {

required init() {
    super.init()
}

required convenience init(node: Node, in context: Context) throws {
    try self.init(node: node, in: context)
}

required convenience init(realm: RLMRealm, schema: RLMObjectSchema) {
    self.init(realm: realm, schema: schema)
}

required convenience init(value: Any, schema: RLMSchema) {
    self.init(value: value, schema: schema)
}

dynamic var identifier = ""
dynamic var updatedAt: Date?

override static func primaryKey() -> String {
    return "identifier"
}

static func newInstance(_ node: Node, context: Context = EmptyNode) throws -> Self {
    let map = Map(node: node, in: context)
    let new = self.init()
    try new.sequence(map)
    return new
}

func sequence(_ map: Map) throws {
    switch map.type {
    case .fromNode:
        if self.identifier.isEmpty {
            // only map id if there isn't one, otherwise Realm complains about modified primaryKey
            try self.identifier <~ map["id"]
        }
        updatedAt = Date()
    case .toNode:
        if !self.identifier.isEmpty {
            try self.identifier ~> map["id"]
        }
    }
}

func objectRepresentation() -> [String : AnyObject] {
    if let result = try? self.toObject() {
        return result as? [String : AnyObject] ?? [:]
    } else {
        return [:]
    }
}

static func objectInRealm(_ realm: Realm, identifier: String?) -> Self? {
    if let identifier = identifier {
        return realm.object(ofType: self, forPrimaryKey: identifier)
    } else {
        return nil
    }
}

static func createOrFindObject(inRealm realm: Realm, identifier: String) -> Self {
    if let foundObject = realm.object(ofType: self, forPrimaryKey: identifier) {
        return foundObject
    } else {
        return realm.create(self, value: ["identifier" : identifier], update: false)
    }
}
}

Upvotes: 1

Views: 1798

Answers (1)

kishikawa katsumi
kishikawa katsumi

Reputation: 10573

You don't need to override init(realm:schema:) and init(realm:schema:), just define convenience required init(node:in:) throws like the following.

class BaseModel : RealmSwift.Object, MappableBase, Identifiable {

    convenience required init(node: Node, in context: Context) throws {
        try self.init(node: node, in: context)
    }

    ...

}

Upvotes: 1

Related Questions