tristanhimmelman/AlamofireObjectMapper

Realm-support

Closed this issue · 3 comments

Is there a trick to save the mapped Realm-Objects to Realm?

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

                if let error = response.result.error{
                    print("Error: \(error.localizedDescription)")
                    errorHandler(error)
                    return
                }

                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)
                    }
                }
        }

In my example I fetch an Array of ParticipantData (Type, Object, Mappable) from the server. I can iterate over the array and print all the mapped properties. Then I store them to realm. But realm saves empty objects - each property is empty. Any idea?

Sorry I'm not sure what the problem you are having is... Were you able to resolve it?

Try this:

let realm = try! Realm()

realm.beginWrite()
for participants in participantsArray
{
    if let data = Mapper<ParticipantData>().map(participants)
    {
        realm.add(data, update: true)
    }
}

try! realm.commitWrite()

Yes my solution is looking like the one from AFcgi. Thanks!