title: Realm with Alamofire and Swift
body: <p>I am making my first attempt at using Realm in Swift. I have my model setup and I successfully receive the response using Alamofire and I map it to the object. I am then attempting to store the data in realm. It creates the object, but the values are the default values and not what is received from the backend. What am I missing?  Here's the model: </p>
  
  <pre><code>import Foundation
  import ObjectMapper
  import RealmSwift
  
  @objcMembers class LoginResponseModel: Object, Mappable {
  
  dynamic var status = ""
  dynamic var id = UUID().uuidString
  dynamic var userId = 0
  dynamic var authToken = ""
  
  required convenience init?(map: Map) {
      self.init()
  }
  
  convenience init(status: String, userId: Int, authToken: String) {
      self.init()
      self.status = status
      self.userId = userId
      self.authToken = authToken
  }
  
  override static func primaryKey() -&gt; String? {
      return "id"
  }
  
  func mapping(map: Map) {
      status &lt;- map["status"]
      userId &lt;- map["user_id"]
      authToken &lt;- map["auth_token"]
  }
  }
  </code></pre>
  
  <p>Here is where I attempt to save it:</p>
  
  <pre><code>APIHelper.shared.login(username: username!, password: password!, success: { (result) in
              if let login = result {
                  if (login.status != "failed") {
                      let userId = login.userId
                      let authToken = login.authToken
                      UserDefaults.standard.setValue(String(userId), forKey: "USERID")
                      UserDefaults.standard.setValue(authToken, forKey: "AUTHTOKEN")
                      UserDefaults.standard.synchronize()
                      vc.modalPresentationStyle = .fullScreen
  
  
  
                      do {
                          let realm = try! Realm()
                          try realm.write {
                              realm.add(login)
                          }
                      } catch(let error) {
                          print(error)
                      }
  
                      InstanceID.instanceID().instanceID { (result, error) in
                          if let error = error {
                              print(error)
                          } else if let result = result {
                              self.sendToken(userId: String(userId), authToken: authToken, fcmToken: result.token)
                          }
                      }
  self.present(vc, animated: true, completion: nil)
                  }
              }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>API call:</p>
  
  <pre><code>public func login(username: String, password: String, success: @escaping(_ response: LoginResponseModel?) -&gt; Void, failure: @escaping(_ error: Error?) -&gt; Void) {
      let url = "\(baseUrl)appLogin"
  
      AF.request(url, method: .post, parameters: ["emailOrPhone": username, "password": password]).responseString { response in
          print(type(of: response).self)
  
          switch response.result {
          case .success(let json):
              let resp = LoginResponseModel(JSONString: json)
              success(resp)
          case .failure(let error):
              failure(error)
          }
      }
  }
  </code></pre>
  
  <p>I get the object here: </p>
  
  <pre><code>let login = realm.objects(LoginResponseModel.self).first
  </code></pre>
  
  <p>Value of object:</p>
  
  <pre><code>status = ""
  id = "1820A5D4-A714-4774-9707-4709ED39B570" 
  userId = 0
  authToken = ""
  </code></pre>