RealmSwift也要求Realm

时间:2017-04-19 15:51:51

标签: swift realm genome

我正在将项目迁移到Swift 3,并遇到了RealmSwift(2.6.1)和Genome(3.2.0)的一些问题。我在Xcode中遇到Realm错误,说我需要这些内容:

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

但是,除了RealmSwift之外,还需要导入Realm,当我的类初始化时,它会尝试使用RLMRealm而不是Realm。警告说'required'初始化程序'init(realm:schema :)'必须由'Object'的子类提供,但是需要init使用RLMRealm而不是Realm任何建议吗?

我正在使用Genome,这需要这个init,这就是为什么Realm首先要求初始化器:

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

所以这些看起来像这样:

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

在没有任何这些初始化程序的情况下,Swift 2.3(使用相应的Swift 2.3版本的Realm和Genome)中的一切工作正常,但现在它无法正常工作。

整个模型:

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

1 个答案:

答案 0 :(得分:1)

您无需覆盖init(realm:schema:)init(realm:schema:),只需定义convenience required init(node:in:) throws,如下所示。

class BaseModel : RealmSwift.Object, MappableBase, Identifiable {

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

    ...

}