You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have some [String: Any] dictionaries and [[String: Any]] arrays coming from JSONSerialization.jsonObject() and we would like to make it class instances of Realm Object models (let's say a PersonModel for below example). According to documentation, we need to design models with properties like that:
class PersonModel : Object {
dynamic var id = ""
dynamic var name = ""
dynamic var imagePath: String?
let friends = List<PersonModel>()
let movies = List<MovieModel>()
}
Our wishes for making the instances
The creation of an instance based on a dictionary should be conditional: i.e. it should return a Person? because sometimes the data is invalid.
The creation of a List based on an array should filter (or flatmap) valid objects only.
The key names of the dictionary and the variable names of the class do not match: i.e. the JSON may have a "username" but the corresponding class variable would be "name". That's why we can't avoid a manual mapping solution of the dictionaries.
no instance creation at all of a "u3" PersonModel, because it has invalid name.
a PersonModel instance with name "bob" and friends being List(max).
a PersonModel instance with name "max" and friends being List(bob).
But it's not obvious to us how we're supposed to achieve those conditions with RealmSwift. We don't see how to write code to workaround the non-fallible constructor Realm.create(_:value:update:). And we don't see how third-parties JSON mappers (like lyft/mapper or JohnSundell/Unbox) can to the job of filtering/converting a given non appropriate [[String:Any]] to a [[String:Any]] compatible with RealmSwift create() method.
The text was updated successfully, but these errors were encountered:
@Coeur We recommend using third-party mapping library to bind JSON to Realm model. I think ObjectMapper https://github.com/Hearst-DD/ObjectMapper provide failable initializer to validate JSON structure while mapping process. Is this answer for you?
There are so many great 3rd party model mapping libraries out there, that we've decided not to build our own into Realm, instead letting you use the framework of your choosing. See #694 (comment) for more.
Context
We have some
[String: Any]
dictionaries and[[String: Any]]
arrays coming fromJSONSerialization.jsonObject()
and we would like to make it class instances of RealmObject
models (let's say aPersonModel
for below example). According to documentation, we need to design models with properties like that:Our wishes for making the instances
Person?
because sometimes the data is invalid.Problems
Realm.create(_:value:update:)
doesn't throw and never returns nil, which means it seems unable to deal with invalid data.Question
Let's say we have this
[[String:Any]]
array of dictionaries:The desired result would be to have:
"bob"
and friends beingList(max)
."max"
and friends beingList(bob)
.But it's not obvious to us how we're supposed to achieve those conditions with RealmSwift. We don't see how to write code to workaround the non-fallible constructor
Realm.create(_:value:update:)
. And we don't see how third-parties JSON mappers (like lyft/mapper or JohnSundell/Unbox) can to the job of filtering/converting a given non appropriate[[String:Any]]
to a[[String:Any]]
compatible with RealmSwiftcreate()
method.The text was updated successfully, but these errors were encountered: