ObjectMapper and Realm's List types

Created on 25 May 2016  路  6Comments  路  Source: tristanhimmelman/ObjectMapper

Realm To-Many Relationships types is List. and ObjectMapper is Array OR Set.
ObjectMapper could support RealmSwift List types?

Most helpful comment

You can use ArrayTransform
items <- (map["items"],ArrayTransform<Item>())

All 6 comments

I have the same problem.
Currently I'm trying the following workaround. I wonder if you have any other ideas until the problem is fixed?

class MyObject: Object, Mappable {
    let tags = List<Tag>()

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

    func mapping(map: Map) {
        var tags: [Tag]?
        tags <- map["tags"]
        if let tags = tags {
            for tag in tags {
                self.tags.append(tag)
            }
        }
    }
}

same for me

You can use ArrayTransform
items <- (map["items"],ArrayTransform<Item>())

There is reference to simple extension that can be used https://github.com/Hearst-DD/ObjectMapper#objectmapper--realm

I guess this issue should be closed. @pawin s solution works perfectly

I've created an operator which properly fixes this issue. Please, check https://gist.github.com/danilValeev/ef29630b61eed510ca135034c444a98a
With this operator you don't need any additional code or transforms.
list <- map["name"]

Here is Swift 3 version. To see the version for Swift 2 and an example of usage please check the gist.

import Foundation
import RealmSwift
import ObjectMapper

infix operator <-

/// Object of Realm's List type
public func <- <T: Mappable>(left: List<T>, right: Map) {
    var array: [T]?

    if right.mappingType == .toJSON {
        array = Array(left)
    }

    array <- right

    if right.mappingType == .fromJSON {
        if let theArray = array {
            left.append(objectsIn: theArray)
        }
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

adasoft-dev picture adasoft-dev  路  3Comments

delbyze picture delbyze  路  3Comments

jperera84 picture jperera84  路  4Comments

pcompassion picture pcompassion  路  3Comments

liltimtim picture liltimtim  路  3Comments