Reputation: 2024
{
"in_app": [
{
"email": "[email protected]",
"active_platforms": [
"email"
],
"type": "primary_email",
"linked_to": {
"_id": "59245c790550bb34cd42e51d",
"company": "ABC Corp",
"image": " ",
"designation": "CEO",
"name": "John Doe"
}
},
{
"email": "[email protected]",
"active_platforms": [
"email"
],
"type": "primary_email",
"linked_to": {
"_id": "5926b5d780ff2ad821a669dab134d",
"company": null,
"image": null,
"designation": null,
"name": null
}
}
]
}
InAppUser Model
class InAppUser: Object, Mappable{
dynamic var email: String = ""
dynamic var type: String = ""
dynamic var linked_to: AppUser?
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
email <- map["email"]
type <- map["type"]
linked_to <- map["linked_to"]
}
}
class AppUser : Object, Mappable {
dynamic var userId: String = ""
dynamic var company: String = ""
dynamic var designation: String = ""
dynamic var name: String = ""
dynamic var image: String = ""
dynamic var email: String = ""
override class func primaryKey() -> String? {
return "email"
}
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
userId <- map["_id"]
company <- map["company"]
designation <- map["designation"]
name <- map["name"]
image <- map["image"]
email <- map["login_email"]
}
}
when I print my entry it shows correct values in first "linked_to" but later when I pull all realm object only ID comes in "linked_to" and other name, etc is null
for [email protected]
.
[email protected]
has name but still its now showing. for [email protected]
it is fine as it is returning null
.
Upvotes: 0
Views: 244
Reputation: 184
you have set the email as a primary key on AppUser and mapped it to "login_email". In your JSON you don't have any "login_email" for the linked_to parts so the primary key is always an empty string (the default you have set is ""). So in fact you are always overriding the same AppUser. The last one in the array has all fields as null and therefore you end up with all users (or actually the only user) having null in all properties.
You should either make sure all "linked_to" users in your json have a "login_email" or change your primary key to something that is actually unique, like the userId.
Upvotes: 1