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