tristanhimmelman / AlamofireObjectMapper

An Alamofire extension which converts JSON response data into swift objects using ObjectMapper
MIT License
2.66k stars 474 forks source link

Multiple POST calls to backend give Error Domain=com.alamofire.error Code=-6004 "ObjectMapper failed to serialize response." #138

Closed CedricVanAsch closed 7 years ago

CedricVanAsch commented 8 years ago

Hi,

So for my app I have to call multiple URL's and their responses in the backend. Here is an example:

import UIKit
import Alamofire
import Realm
import RealmSwift
import AlamofireObjectMapper

class SyncFavoriteProducers {

    static let shared = SyncFavoriteProducers()  // singleton object

    func SyncWithBackend(token token: String) {

        let realm = try! Realm()
        let URL = "SorryCantShowThisHere" + token

        let getFavoriteArticlesFromApp = realm.objects(Producer).filter("favorite == true")        
        //map results from filter
        let mapped = getFavoriteArticlesFromApp.map({ $0 })

        // create dictionary of String / AnyObject
        var dictionary: [String : AnyObject] = [:]
        // create empty array of dictionaries
        var ArrayOfDictionaries = [[String : AnyObject]]()
        for folder in mapped {
            dictionary["id"] = folder.id
            dictionary["favorite"] = folder.favorite
            ArrayOfDictionaries.append(dictionary)
        }
        print(ArrayOfDictionaries)

        let parameters = [
            "result": ArrayOfDictionaries
        ]

        let request = Alamofire.request(.POST, URL , parameters: parameters, encoding: .JSON)
        request.validate()

        request.responseObject { (response: Response<ProducerResults, NSError>) in
            if response.result.isSuccess {
                let mappedObject = response.result.value
                let realm = try! Realm()
                try! realm.write {
                    if let everythingUnderResult = mappedObject?.result {
                        for item in everythingUnderResult {
                            if let currentObject = realm.objectForPrimaryKey(Producer.self, key: item.id) {
                                item.logoUrl = currentObject.logoUrl
                                item.siteUrl = currentObject.siteUrl
                            }
                            item.favorite = true
                            realm.add(item,update: true)
                        }
                    }
                }
            } else {
                print("not successfull")
                print(response.result.error)
            }
        }  
    }    
}

Now here is the real issue: When I do these calls one by one, it works (everything gets mapped etc.) BUT when I do all the calls at once (total of 5 calls), then I get the following message: Error Domain=com.alamofire.error Code=-6004 "ObjectMapper failed to serialize response." UserInfo={NSLocalizedFailureReason=ObjectMapper failed to serialize response.})

How is it possible that they work one by one, but not when I try to run them all at the 'same' time.

    func syncFavorites() {
        let realm = try! Realm()
        let getCurrentToken = realm.objects(User).first?.token

        guard getCurrentToken != nil else {
            print("Object is nil")
            return
        }
        SyncFavoriteFolders.shared.SyncWithBackend(token: getCurrentToken!)
        SyncFavoriteProducers.shared.SyncWithBackend(token: getCurrentToken!)
        SyncFavoriteArticles.shared.SyncWithBackend(token: getCurrentToken!)
        SyncFavoriteBrands.shared.SyncWithBackend(token: getCurrentToken!)
        SyncFavoriteCategories.shared.SyncWithBackend(token: getCurrentToken!)
    }
tristanhimmelman commented 7 years ago

I'm not sure what the issue is here. Seems like it may have to do with the backend... I am closing this for now. Feel free to comment if you found a solution or are still having the problem.