Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do failable mapping of Dictionaries/Arrays to RealmSwift Objects #4458

Closed
Coeur opened this issue Dec 17, 2016 · 3 comments
Closed

How to do failable mapping of Dictionaries/Arrays to RealmSwift Objects #4458

Coeur opened this issue Dec 17, 2016 · 3 comments
Assignees
Labels

Comments

@Coeur
Copy link

Coeur commented Dec 17, 2016

Context

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.

Problems

Question

Let's say we have this [[String:Any]] array of dictionaries:

[["userid": "u1",
  "username": "bob",
  "image": "http://example.com/bob.jpg",
  "friendIds": ["u2", "u3"],
  "movieIds": ["sw4", "sw5", "sw6", "ro"]],
 ["userid": "u2",
  "username": "max",
  "image": "http://example.com/max.jpg",
  "friendIds": ["u1", "u3"],
  "movieIds": ["sw4", "sw5", "sw6", "sw7"]],
 ["userid": "u3",
  "username": false],
]

The desired result would be to have:

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

@kishikawakatsumi
Copy link
Contributor

@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?

@jpsim
Copy link
Contributor

jpsim commented Dec 19, 2016

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.

@TimOliver
Copy link
Contributor

Thanks for the issue @Coeur! I hope that answered your question.
Please give ObjectMapper a try and see if that provides the functionality you need.

Have a happy holiday! :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants