0xjmp / ObjectMapper-Realm

An ObjectMapper extension that serializes arbitrary JSON into Realm's List class
Other
178 stars 43 forks source link

What About MapContext? #29

Closed Busta117 closed 5 years ago

Busta117 commented 6 years ago

there is no way to send a context to the List Transform...

Busta117 commented 6 years ago

using this https://gist.github.com/Busta117/78a1aded0a73cf1c4f61bfda113a86c8 is no needed the ListTransform and no need to care about context

danipralea commented 5 years ago

care to develop on the usage @Busta117 ? I'm also curious on how I can get more context out of List mapping

Busta117 commented 5 years ago

@danipralea adding that file (link i posted) to your project and in the method mapping just use it like any other mapping

func mapping(map: Map) {
    variableList <- map["keyOnJson"]
}
danipralea commented 5 years ago

thanks @Busta117 for your reply. I meant can I have any information that a child is being mapped inside a list vs. mapping on its own?

Busta117 commented 5 years ago

@danipralea i don't understand your question, every child/element on a list is mapping in the same way, go thru the method mapping of the every child and it will mapping it, you can just put a breakpoint on the mapping method of the child and you will see that it is mapped... object mapper get an array from the json response, this method(link) just convert that array of elements into a realm list to allow it to save it on the database

0xjmp commented 5 years ago

As of v0.9, you may use the onSerialize method to control whatever context you would like:

class User: Object, Mappable {

    func mapping(map: Map) {
        username              <- map["username"]
        friends               <- (map["friends"], ListTransform<User>(onSerialize: onSerialize))
    }

    private func onSerialize(users: List<User>) {
        let realm = Storage.shared.realm
        try! realm.write {
            realm.add(users, update: .modified)
        }
    }
}