Realm Swift returns objects with all nil values

I use ObjectMapper to parse JSON objects in Realm.

My Trip class is as follows:

class Trip: Object, Mappable { dynamic var Id : String? = nil dynamic var CreatedOn : String? = nil dynamic var LastModified : String? = nil required convenience init?(_ map: Map) { self.init() } func mapping(map: Map) { Id <- map["Id"]; CreatedOn <- map["CreatedOn"]; LastModified <- map["LastModified"]; } } 

I make a web service request using Alamofire:

 Alamofire.request(.GET, path, headers: ["Token" : auth_token]).responseJSON { response in let dict : NSDictionary? = response.result.value as? NSDictionary let test = Mapper<Trip>().map(dict!) let realm = try! Realm() realm.beginWrite() realm.add(test!) try! realm.commitWrite() let alltrips : Results<Trip> = realm.objects(Trip) let firstTrip = alltrips.first } 

In the above code, when I print the test, I get:

 (AwesomeApp.Trip?) test = 0x0000000154e8f0d0 { RealmSwift.Object = { Realm.RLMObjectBase = { ObjectiveC.NSObject = {} } } Id = "47d86d34-b6f2-4a9f-9e31-30c81a915492" CreatedOn = "2016-01-20T23:39:41.995Z" LastModified = "2016-01-20T23:44:39.363Z" } 

When I print, firstTrip, I get

 (AwesomeApp.Trip?) firstTrip = 0x0000000154f1f370 { RealmSwift.Object = { Realm.RLMObjectBase = { ObjectiveC.NSObject = {} } } Id = nil CreatedOn = nil LastModified = nil } 

I used the Realm browser and it looks like the values ​​were correctly written to the database. However, reading the values ​​returns a trip object with all nil values. Why is this?

EDIT : I printed allTrips with print (allTrips) and this is printed:

 Results<Trip> ( [0] Trip { Id = 47d86d34-b6f2-4a9f-9e31-30c81a915492; CreatedOn = 2016-01-20T23:39:41.995Z; LastModified = 2016-01-20T23:44:39.363Z; } ) 
+5
source share
1 answer

Realm Object subclass instance variables are used only for objects that are not yet added to Realm. After an object has been added to Realm or for an object that has been obtained from Realm, the recipient objects and object setters access the data directly from Realm without using instance variables. This is why instance variables do not have the expected values.

+2
source

Source: https://habr.com/ru/post/1243198/


All Articles