Search code examples
iosjsonswiftrealm

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?


Solution

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