Search code examples
iosswift3realmrealm-migrationrealm-list

iOS Swift Storing Alamofire Response To Realm


I need to store alamofire JSON response to realm storage directly. Here is the response tat I get from alamofire JSON.

{
  "all": [
    {
      "date": "2017-03-30T00:00:00.000Z",
      "subject": "loe",
      "desc": "",
      "id": 13,
      "number": "19/312/2012",
      "title": "loe",
      "name": "Supreme",
      "type": "Event",
      "practice": 20,
      "contact": "",
      "object": "{\"id\":20,\"id\":13,\"name\":\"loe\",\"time\":\"2017-03-30T00:00:00.000Z\",\"end\":\"2017-03-31T00:00:00.000Z\",\"creator\":\"user\",\"created_by\":132,\"des\":\"\",\"created_at\":\"2017-03-30T08:22:31.150Z\",\"updated_at\":\"2017-03-30T08:23:04.944Z\",\"judge\":null,\"purpose\":null,\"google_event_id\":null,\"is_completed\":false,\"business\":null,\"last_notified\":\"2017-03-30T08:23:04.926Z\",\"next\":null,\"business_date\":null,\"business\":false}"
    },
    {
      "date": "2017-03-30T00:30:00.000Z",
      "subject": "user",
      "desc": "Loe",
      "id": 138,
      "number": "19/312/2012",
      "title": "loe user",
      "name": "Supreme India",
      "type": "Appointment",
      "practice": 6,
      "contact": 91,
      "object": "{\"id\":20,\"id\":13,\"name\":\"loe\",\"time\":\"2017-03-30T00:00:00.000Z\",\"end\":\"2017-03-31T00:00:00.000Z\",\"creator\":\"user\",\"created_by\":132,\"des\":\"\",\"created_at\":\"2017-03-30T08:22:31.150Z\",\"updated_at\":\"2017-03-30T08:23:04.944Z\",\"judge\":null,\"purpose\":null,\"google_event_id\":null,\"is_completed\":false,\"business\":null,\"last_notified\":\"2017-03-30T08:23:04.926Z\",\"next\":null,\"business_date\":null,\"business\":false}"
    }

And Here is the code that I have used for realm storage

class PracticeArea:Object,Mappable
{
    dynamic var contact = 0
    dynamic var id = ""
    dynamic var number = ""
    dynamic var title = ""
    dynamic var name = ""
    dynamic var date = ""
    dynamic var description2 = ""
    dynamic var object = ""
    dynamic var practice = 0
    dynamic var subject = ""
    dynamic var type = ""

    override static func primaryKey() -> String?
    {
        return "contact"
    }

    //Impl. of Mappable protocol
    required convenience init?(map: Map)
    {
        self.init()
    }

    func mapping(map: Map)
    {
        contact    <- map["contact"]
        id <- map["id"]
        number <- map["number"]
        title <- map["title"]
        name <- map["name"]
        date <- map["date"]
        description2 <- map["description"]
        object <- map["object"]
        practice <- map["practice"]
        subject <- map["subject"]
        type <- map["type"]
    }
}

Code Used for Storing Values:

let PracticeTestValues = response.result.value!
let realm:Realm = try! Realm()
                        try! realm.write
                        {
                                for all in PracticeTestValues as! [Any]
                                {
                                    realm.add(all as! Object, update: true)
                                }
                        }

Solution

  • I would sugget to use this:

    for all in PracticeTestValues as? NSArray
     {
        realm.add(all as! Object, update: true)
      }