tristanhimmelman/AlamofireObjectMapper

responseArray Serialisation Errors

SigmundRakes opened this issue · 3 comments

Hi all,

Having trouble mapping an API into a Realm Object. Keep getting "ObjectMapper Failed to serialise response"

Here is my code for the API call func:

class GetData {

    static func getCall <T: Object> (type: T.Type, headers: HTTPHeaders =
        [   "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": "API special key",], success:() -> Void, fail:@escaping (_ error:NSError)->Void) where T:Mappable, T:Meta {
        
        Alamofire.request(type.url(), method: .get).responseJSON { (response: DataResponse<[T]>) in
            switch response.result {
            case .success(let items):
                autoreleasepool {
                    do {
                        let realm = try Realm()
                        try realm.write {
                            for item in items {
                                realm.add(item, update: true)
                            }
                        }
                    } catch let error as NSError {
                        fail(error)
                    }
                }
            case .failure(let error):
                fail(error as NSError)
            }
        }
        
    }

AND the code for the model:

import UIKit
import Firebase
import Alamofire
import AlamofireObjectMapper
import ObjectMapper
import RealmSwift


protocol Meta {
    static func url() -> String
}

//MARK: The user model for API calls & Realm
class UserModel: Object, Mappable, Meta {
    
    var id: String? = nil
    var first: Sr
    var second: String? = nil
    var third: String? = nil
    var fourth: String? = nil
    let userid = Auth.auth().currentUser?.uid
    
    var name: String? = nil
    var email: String? = nil
    var ppic: String? = nil
    var dob: String? = nil
    var addressL1: String? = nil
    var addressL2: String? = nil
    var addressL3: String? = nil
    
    override static func primaryKey() -> String? {
        return "id"
    }
    
    //Impl. of Mappable protocol
    required convenience init?(map: Map) {
        self.init()
    }
    
    func mapping(map: Map) {
        firstid <- map["first_id"]
        
        name <- map["name"]
        email <- map["email"]
        dob <- map["d_o_b"]
        
        addressL1 <- map["address"]
        addressL2 <- map["address_2"]
        addressL3 <- map["address_3"]
        
    }
    
    //Impl. of Meta protocol
    static func url() -> String {
        return "https://example.com"
    }
    
}

Any help will be greatly appreciated!
Thanks

Solved: Wrong function call. Needed to call responseObject rather than responseArray as JSON output from API is an Object with nested Arrays and not an Array

New problem -> Null output of mapped JSON values using responseObject. Will open a new issue.

New problem -> Null output of mapped JSON values using responseObject. Will open a new issue.

solved -> misplaced for x in y loop