0
votes

I am using alamofire for service integration. its giving an empty reponse as [] sometimes only. in that time app is crashing because of empty array response. How to handle that response in swift iOS.

Here is my code:

    let headers = ["Authorization" : "Bearer "+token,
                   "Content-Type": "application/json"]

Alamofire.request("http://sos.partnersbuddy.in/api/friend_request/received", method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON { response in // print("Request (response.request)")

        print("RESPONSE \(String(describing: response.result.value))")
        print("RESPONSE \(response.result)")
        print("RESPONSE \(response)")

            var respVO:[RequestResvo] = Mapper<RequestResvo>().mapArray(JSONArray: response.result.value as! [[String : Any]])

            print(respVO)

            self.postID = respVO[0].id!

            if let result = response.result.value {
                let JSON = result as! NSArray
                print(JSON.value(forKey: "user"))

                let res = JSON.value(forKey: "user")
                let respVo = Mapper<SubRequestVo>().mapArray(JSONArray: res as! [[String : Any]])

                for (index, element) in (respVo.enumerated()) {
                    print(index)

                    self.nameArr.append(element.first_name!)
                    self.numberArr.append(element.mobile!)

                    print("nameArr\(self.nameArr)")                        
                    print("numberArr\(self.numberArr)")

                    DispatchQueue.main.async {
                        // update your UI and model objects here

                        self.mytableView.reloadData()
                        // SKActivityIndicator.dismiss()
                    }
                }
            }
    }

How to handle this empty response in that time app is crashing because of empty array response. How to handle that response in swift iOS.

3

3 Answers

1
votes

You can try print response as string:

Add it at the end of your request, like this:

Alamofire.request(url, method: .get, encoding: JSONEncoding.default, headers: headers)
.responseJSON { response in

}.responseString { response in
    print(response)
}
0
votes

Looking at your code, I would do something like this:

Alamofire.request("http://sos.partnersbuddy.in/api/friend_request/received", method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON { response in

        switch response.result {
        case .success(let json):
            guard let array = json as? [[String : Any]] else {
                // Handle error: your json response is not a [[String : Any]] as you were expecting
            }

            if array.count == 0 {
                // Handle empty array response
            }

            var respVO:[RequestResvo] = Mapper<RequestResvo>().mapArray(JSONArray: response.result.value as! [[String : Any]])

            print(respVO)

            self.postID = respVO[0].id!

            if let result = response.result.value {
                let JSON = result as! NSArray
                print(JSON.value(forKey: "user"))

                let res = JSON.value(forKey: "user")
                let respVo = Mapper<SubRequestVo>().mapArray(JSONArray: res as! [[String : Any]])

                for (index, element) in (respVo.enumerated()) {
                    print(index)

                    self.nameArr.append(element.first_name!)
                    self.numberArr.append(element.mobile!)

                    print("nameArr\(self.nameArr)")
                    print("numberArr\(self.numberArr)")

                    DispatchQueue.main.async {
                        // update your UI and model objects here

                        self.mytableView.reloadData()
                        // SKActivityIndicator.dismiss()
                    }
                }
            }
        case .failure(let error):
            // Handle response error
            print(error)
        }
    }

This let you handle both response result and the empty array case. Giving more information about your response json format, would be better to write down more precise code that fit your problem.

0
votes

I got an empty json from alamofire response with nil status code and failure because of sending the URL with spaces. If you got something similar, make sure to use the url with allowed characters:

var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)