Could you help me please with this
I'm trying to pull data from API using Alamofire. Is it possible to pull simple data like ["title"] and Image in the same request and populate it in table view?
Here's my code:
Alamofire.request(baseURL).responseJSON { (response) in
switch response.result {
case .success:
if let value = response.result.value{
let json = JSON(value)
json.forEach({ (temp) in
let title = temp.1["title"].stringValue
let run = temp.1["run"].stringValue
let image = temp.1["thumb"].stringValue
let newmodel = Schedule(title: title, run: run, image: image)
self.scheduleArray.append(newmodel)
})
}
case .failure(let error):
print(error.localizedDescription)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
You can get the base data at first and after that get every image with single request
get base data with this code :
func POSTWithLoad(urlString: String,paramaters: [String: AnyObject]? = nil,success:@escaping (_ responseObject: AnyObject?) -> Void ,failure: @escaping (_ error: String?) -> Void ) {
Alamofire.request(baseUrlDev+urlString, method: .post, parameters:paramaters, encoding: URLEncoding.default, headers: headers).responseJSON(completionHandler: { (responseData) in
if (responseData.result.error == nil && responseData.result.isSuccess == true) {
//success
success(responseData)
} else {
//fauiler
failure("UnKnowen Data")
}
})
}
and use this for getImage :
func getImage(link : String , completion : @escaping (_ imageWithLink : (link : String,image : UIImage)) -> Void){
var image = UIImage(named: "placeholder")
URLSession.shared.dataTask(with: NSURL(string: link.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print("errorLoadIamge : "+link)
//completion(image)
print(error)
return
}
DispatchQueue.main.async(execute: { () -> Void in
let mimage = UIImage(data: data!)
if(mimage != nil){
image = mimage!
completion((link : link , image : image))
print("successLoadIamge : "+link)
return
}
})
}).resume()
}
then can use it in your ViewContoller like this :
let products : [Product] = []
let productImages : [String] = []
POSTWithLoad(urlString: "YApiLink" , paramaters: requestParameters as [String : AnyObject]?, success: { (successObject) in
products = Mapper<Product>().mapArray(JSONObject : successObject)
productsImages = []
print(successObject)
}, failure :{ (failureObject) in
print(failureObject)
}
)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let product = products[indexPath.row]
let cell = YourTableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableViewCell
if let imageAndLink = productsImages.first(where: { $0.link == cell.product?.productImage})
{cell.pImage?.image = imageAndLink.image}
else{ cell.pImage.image = UIImage(named: "placeholder")
getImage(link: cell.product?.productImage ?? "", completion: {(newImage) in
DispatchQueue.main.async {
if self.productsImages.first(where: { $0.link == newImage.link}) == nil
{
self.productsImages.append(ProductImage(link : newImage.link , image : newImage.image) )
}
if cell.product?.productImage == newImage.link{
cell.pImage.image = newImage.image
}
}
})
}
}