将数据快速存储在领域数据库中

时间:2019-03-19 19:04:25

标签: ios swift database list realm

因此,我第一次尝试在我的应用程序中使用领域数据库。我正在尝试在数据库中创建关系。

这是json

{
            "id": 1,
            "name": "Definizioni generali - Doveri nell'uso della strada",
            "image": "559",
            "video": "0",
            "created_at": "2019-03-14 22:27:23",
            "updated_at": "2019-03-14 22:27:23",
            "sections": [
                {
                    "id": 3110,
                    "chapter_id": 1,
                    "name": "a) Definizioni stradali e di traffico",
                    "created_at": "2019-03-14 22:30:52",
                    "updated_at": "2019-03-14 22:30:52"
                },
                {
                    "id": 3123,
                    "chapter_id": 1,
                    "name": "b) Definizioni e classificazione dei veicoli",
                    "created_at": "2019-03-14 22:30:52",
                    "updated_at": "2019-03-14 22:30:52"
                },
                {
                    "id": 3131,
                    "chapter_id": 1,
                    "name": "c) Doveri del conducente nell'uso della strada - Convivenza civile e uso responsabile della strada",
                    "created_at": "2019-03-14 22:30:52",
                    "updated_at": "2019-03-14 22:30:52"
                },
                {
                    "id": 3137,
                    "chapter_id": 1,
                    "name": "d) Riguardo verso gli utenti deboli della strada",
                    "created_at": "2019-03-14 22:30:52",
                    "updated_at": "2019-03-14 22:30:52"
                }
            ]
        }

这是我的章节课程

class Chapter: Object, Mappable {
@objc dynamic var id:Int = -1
@objc dynamic var name: String = kBlankString
@objc dynamic var image: String = kBlankString
@objc dynamic var video: String = kBlankString
@objc dynamic var createdAt: String = kBlankString
@objc dynamic var updatedAt: String = kBlankString
var sections = List<Section>()

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

required convenience init?(map: Map) {
    self.init()
}

func mapping(map: Map) {
    id <- map["id"]
    name <- map["name"]
    image <- map["image"]
    video <- map["video"]
    createdAt <- map["created_at"]
    updatedAt <- map["updated_at"]
    sections <- map["sections"]


}

}

此行sections <- map["sections"]

我添加了一个断点并检查了po map["sections"].JSON

它在这里显示数据。

问题是章节存储在数据库中,但是每个章节的节列表都为空,这是不正确的

这是节类:

class Section: Object, Mappable {
@objc dynamic var id: Int = -1
@objc dynamic var chapterID: Int = -1
@objc dynamic var name: String = kBlankString
@objc dynamic var createdAt: String = kBlankString
@objc dynamic var updatedAt: String = kBlankString

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


required convenience init?(map: Map) {
    self.init()
}

func mapping(map: Map) {
    id <- map["id"]
    chapterID <- map["chapter_id"]
    name <- map["name"]
    createdAt <- map["created_at"]
    updatedAt <- map["updated_at"]

}

}

如果我做错了什么,请指导我,这是我第一次尝试使用领域数据库。谢谢

1 个答案:

答案 0 :(得分:1)

首先,我假设这是使用ObjectMapper(https://github.com/tristanhimmelman/ObjectMapper)的,我自己并没有使用过。

但是问题是您试图为这些部分制作的地图将为您提供数组类型(据我所知),但是Realm不支持数组(如您所知,您已经正确使用了List

我认为您需要自定义转换(https://github.com/tristanhimmelman/ObjectMapper#custom-transforms)才能转换为List<>类型。 RealmAdditions显示了如何执行此操作。这显示了等效的行来执行示例代码中您想要的操作-重要的一点是该行(转换为使用您的类型):

        sections <- (map["sections"], ListTransform<Section>())