Search code examples
swiftrealmalamofire

Alamofire, ObjectMapper, Realm --> Save Responsedata in Realm


In my app I'm using Alamofire, ObjectMapper and Realm. I want to achieve, that the data (an array of objects) gets persisted in realm after the response was mapped. I have created an class of type Object, Mappable and the mapping is working. The data is written in realm and there is no error in realm in the write transaction. If I query the data from realm I get the objects I have saved but each property is empty. Do you have an idea why the data is not saved when the mapping is successful?

AlamofireManager.Configured
            .request(.GET, URLs.sharedInstance.getContactsUrl())
            .responseArray("contactHeaders") { (response: Response<[ParticipantData], NSError>) in


                if let participantsArray = response.result.value{
                    successHandler(participantsArray)

                    do{
                        try self.realm.write{
                            self.realm.add(participantsArray, update: true)
                        }
                    }
                    catch let err as NSError {
                        print("Error with realm: " + err.localizedDescription)
                    }
                }

Solution

  • When defining your Realm data model classes, you must ensure that their properties are declared dynamic. This exposes the properties to ObjC code and allows the values to be persisted.