RealmSwift链接对象和可解码 [英] RealmSwift LinkingObjects and Decodable

查看:117
本文介绍了RealmSwift链接对象和可解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Realm模型类,我需要将其解码,以便可以从JSON序列化并将其保存到数据库.每个PortfolioItem都与一个Product相关联,在某些时候,我需要通过逆关系从Product到达PortfolioItem.这就是为什么我拥有LinkingObjects属性.问题是当我尝试遵守Decodable协议时.编译器给我一个错误Cannot automatically synthesize 'Decodable' because 'LinkingObjects<PortfolioItem>' does not conform to 'Decodable'.怎么处理呢?我在网上很少了解LinkingObjects和Decodable,也不知道如何解决这个问题.

I have a Realm model class that I need to be Decodable so I can serialize it from JSON and save it to database. Every PortfolioItem is associated with one Product and at some point I need to get to PortfolioItem from Product via inverse relationship. That's why I have LinkingObjects property. The problem is when I try to conform to Decodable protocol. The compiler is giving me an error Cannot automatically synthesize 'Decodable' because 'LinkingObjects<PortfolioItem>' does not conform to 'Decodable' . How to deal with this? I found very little about LinkingObjects and Decodable online and I have no idea how to approach this.

class PortfolioItem: Object {

    @objc dynamic var id: String = ""
    @objc dynamic var productId: String = ""

    @objc dynamic public var product: Product?

    convenience init(id: String, productId: String) {
        self.init()
        self.id = id
    }

}

final class Product: Object, Decodable {

    @objc dynamic var id: String = ""
    @objc dynamic var name: String = ""

    private let portfolioItems = LinkingObjects(fromType: PortfolioItem.self, property: "product")

    public var portfolioItem: PortfolioItem? {
        return portfolioItems.first
    }

    convenience init(id: String, name: String) {
        self.init()
        self.id = id
    }
}

非常感谢Chris Shaw帮助我解决了这个问题.我写了一篇更深入的文章,介绍如何设置Decodable和LinkingObjects,

Big thanks to Chris Shaw for helping me figure this out. I wrote a more in-depth article how to setup Decodable and LinkingObjects, look HERE.

推荐答案

好,除非我丢失了某些内容,否则解码中不需要包含LinkingObjects属性.

Well unless I'm missing something then the LinkingObjects properties does not need to be included in the decoding.

我在这里的假设是,您正在从某个在线来源接收JSON,其中Product的JSON由{id:",name:"}组成.只要您使用关联的Product正确创建了PortfolioItem,那么生成的LinkingObjects属性就是Realm中动态查询的结果(因此可以在没有任何JSON源的情况下使用).

My assumption here is that you're receiving JSON from some online source, where the JSON for a Product consists of { id: "", name: "" }. As long as you're creating the PortfolioItem correctly with associated Product, then the resulting LinkingObjects property is the result of a dynamic query within Realm (and will thus work without any JSON source).

我今天无法测试编译答案,但是您应该能够使用CodingKeys来简单地排除该属性,即将其添加到Product:-

I'm not in a position to test compile an answer today, but you should be able to use CodingKeys to simply exclude that property, i.e. by adding this to Product:-

private enum CodingKeys: String, CodingKey {
    case id
    case name
}

也是无关的,但是请注意,您的convenience init函数不会初始化要传递的所有属性.

Also, unrelated, but note that your convenience init function are not initialising all properties that you're passing in.

这篇关于RealmSwift链接对象和可解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆