Ian van Wijk
Ian van Wijk

Reputation: 159

Realm Object empty when converting using EVReflection

I have a issue when converting a Realm object to a jsonString. I searched online but couldn't find a clear answer. I have a guess what might be the issue...

I have a User Class:

import RealmSwift
import EVReflection

public class User: Object, EVReflectable, IModel {
    dynamic var id: String = UUID().uuidString

    dynamic var createdAt: String = ""
    dynamic var lastUpdate: String = ""

    dynamic var firstName: String = ""
    dynamic var lastName: String = ""

    dynamic var email: String = ""
}

When I try to load the user from my realm database like so:

let users = realm.objects(User.self).filter("id = %@", userId)
let user = users.first

I can access the object without a problem. I see all the data that are stored in the object.

If I want to convert the data to a JSON string and output it I get an empty JSON object:

print(user.toJsonString())

./output

{
   "id":"64EA49AA-333C-4C76-B92C-464EFE1D9E10",
   "createdAt":"",
   "lastUpdate":"",
   "firstName":"",
   "lastName":"",
   "email":""
}

Is this a bug or a Realm related behaviour. Because the only explanation I can find that explains this kind of behaviour is this issue: Github Realm Issue

If this is caused by Realm, does anyone have a solution on how we can fix this problem?

Upvotes: 0

Views: 141

Answers (2)

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

In EVReflecdtion a Realm Object will now implement the EVCustomReflectable protocol by default so that it's able to get the realm properties. Please update to the latest version. Be aware that it will now also use Realm 3.

Upvotes: 1

Guillermo Alvarez
Guillermo Alvarez

Reputation: 1775

This sounds like it might be an issue with the EVReflection library implementation. I had an issue a while back with using ObjectMapper and Realm Lists where it was challenging to make them work nicely together. It might be a good idea to step through the toJsonString() implementation and see if you can figure out what is going on there.

Of course this is all assuming you actually set the properties somewhere (which is not clear from your code snippet above)

Upvotes: 0

Related Questions