0
votes

enter image description here

In the home screen section I have three different UICollection view two of them(Top News and Accommodation) getting data from API and last one(Category) have static data and the problem is that while loading the data from API even I am not able to scroll the static section of UICollection view cell but as data loading complete every thing working fine I am not able to find the problem's solution please help me

override func viewDidLoad() { super.viewDidLoad()

       topNewCV.delegate = self
        topNewCV.dataSource = self

        accommodationCV.delegate = self
        accommodationCV.dataSource = self

        categoryCV.dataSource = self
        categoryCV.delegate = self

  //Loading getNearByPlace function
        self.getNearByPlace()  
}

//cellForItemAt indexPath function

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if collectionView == accommodationCV {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AccommodationCollectionViewCell", for: indexPath) as! AccommodationCollectionViewCell
                cell.titleContainer.text = self.accommodationObject.titleArray[indexPath.row]

        if self.accommodationObject.titleArray.count == self.accommodationObject.imgArray.count {
        if let img = cache.object(forKey: self.accommodationObject.imgArray[indexPath.row] as AnyObject) {
            DispatchQueue.global().async {
                DispatchQueue.main.async {
                    cell.imgContainer.image = img as? UIImage
                }
            }
        } else {
            DispatchQueue.global().async {
                DispatchQueue.main.sync {
                    cell.imgContainer.image = UIImage(url: URL(string: "\(self.accommodationObject.imgArray[indexPath.row])"))
                    self.cache.setObject(UIImage(url: URL(string: "\(self.accommodationObject.imgArray[indexPath.row])"))!, forKey: self.accommodationObject.imgArray[indexPath.row] as AnyObject)
                }
            }
        }
        } else {
            print("Both have not equal data")
        }

        return cell
    } else if collectionView == categoryCV {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCollectionViewCell", for: indexPath) as! CategoryCollectionViewCell

                cell.categorymodel = self.categoryModels?[indexPath.item]

        if indexPath.row % 2 == 0 {
            cell.categoryCVViewContainer.backgroundColor =  colorLiteral(red: 0.3333333333, green: 0.7844525506, blue: 0.6620362924, alpha: 1)
        } else {
             cell.categoryCVViewContainer.backgroundColor =  colorLiteral(red: 1, green: 0.4039215686, blue: 0.4039215686, alpha: 1)
        }
        return cell
    }
    return cell
}

// this fun is for getting data from api

func getNearByPlace() {

    var strGoogleApi = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(user_latitude!), \(user_longitude!)&radius=1000&keyword=hotel&sensor=true&key=abc”
    strGoogleApi = strGoogleApi.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
    print(strGoogleApi)
    var urlRequest = URLRequest(url: URL(string: strGoogleApi)!)
    urlRequest.httpMethod = "GET"

    let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
        if error == nil {
            if let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [String: Any]{

                if let allResults = json!["results"] as? [[String: Any]] {
                    print(allResults)

                    for result in allResults {

                        var geometry = [String: Any]()
                        geometry = result["geometry"] as! [String: Any]
                        var location = [String: Any]()
                        location = geometry["location"] as! [String: Double]

                        self.latitudeArray.append(location["lat"] as! Double)
                        self.longitudeArray.append(location["lng"] as! Double)

                        let name = result["name"]
                        var  image = [[String: Any]]()

                        if result["photos"] != nil {
                        image = result["photos"] as! [[String: Any]]
                            var img = image[0]
                            let url = self.getImageFromApi(image: img["photo_reference"] as! String)
                            self.imgReferenceArray.append(url)
                        } else {
                            self.imgReferenceArray.append(self.icons)
                        }

                        let place_id = result["place_id"]
                        let address = result["vicinity"]

                        if result["name"] != nil {
                            self.nameArray.append(name as! String)
                            self.accommodationObject.titleArray = self.nameArray
                        }
                        if result["place_id"] != nil {
                            self.placeIdArray.append(place_id as! String)
                        } else if result["vicinity"] != nil {
                            self.addressArray.append(address as! String)
                        }
                    }
                }
            }
            OperationQueue.main.addOperation({
                if self.nameArray.count != 0 {
                    DispatchQueue.main.async {
                        self.accommodationCV.reloadData()
                        self.categoryCV.reloadData()
                    }
                }
            })
            self.accommodationObject.imgArray = self.imgReferenceArray
        }
    }
    task.resume()
}
1

1 Answers

0
votes

Some very basic tips:

a) take in account you are dealing with multiple threads.. so adding to arrays must be done with a lot of care.

b) STOP previous calls of "task" var if reloading.. for example saving task in an instance var: call task.cancel()