title: UICollectionView not load new data when make another API Calls Swift
body: <p>Here I'm trying to load the collectionView with another data that fetch from API Calls. At first, I call the data load into CollectionView and it works but when scroll until the bottom I call a method in scrollview to fetch more data, the data is return but the collection view is not updating. I have a struct that manage API calls and it will pass to another VC via delegate and append it into array.</p>
  <p><strong>What did I expect</strong></p>
  <ul>
  <li>Another data that get from API calls will load into collectionView</li>
  </ul>
  <p><strong>What did actually happend</strong></p>
  <ul>
  <li>Data only able to load at the first call and when try to fetch another call, collectionView do not update and keep refresh the first data that get from earlier API calls</li>
  </ul>
  <p><strong>Here's my class that manage networking</strong></p>
  <p><strong>NewsManager.swift</strong></p>
  <pre><code>import UIKit
  
  protocol NewsManagerDelegate {
      func didSendNewsData (_ newsManager: NewsManager, with news : [NewsModel])
  }
  
  
  struct NewsManager {
      
      var delegate : NewsManagerDelegate?
  
      let newsUrl = &quot;https://gnews.io/api/v4/search?q=&quot;
      let key = &quot;APIKEY&quot;
      
      func fetchNews(topics : String = &quot;Apple&quot;,page:Int = 1){
          let urlString = &quot;\(newsUrl)\(topics)&amp;lang=en&amp;page=\(page)&amp;token=\(key)&quot;
          print(urlString)
          performRequest(with : urlString)
      }
  
      
      func performRequest(with urlString : String){
          
          
          // 1. create URL object
          guard let url = URL(string: urlString) else {fatalError(&quot;There's problem to fetch data from this url&quot;)}
          
          // 2. create session, object to do networking
          let session = URLSession(configuration: .default)
          
          //3. give session a task
          let task = session.dataTask(with: url) { (data, response, error) in
              
              if error != nil{
                  print(&quot;Error in giving session a task \(String(describing: error?.localizedDescription))&quot;)
                  
              } else{
                  // determine if data is exist
                  if let safeData = data {
                      //convert data to string
                      //let dataString = String(data: safeData, encoding: .utf8)
                      //print(dataString)
                      
                      // Parse the data here
                      guard let news = parseJSON(safeData) else{ fatalError(&quot;Error parsing data from JSON&quot;)}
                     
                      DispatchQueue.main.async {
                          delegate?.didSendNewsData(self, with: news)
                      
                      }
                      
                  }
              }
          }
          //start the task
          task.resume()
      }
      
      func parseJSON(_ newsData : Data) -&gt; [NewsModel]?{
          
          
          let decoder = JSONDecoder()
          decoder.dateDecodingStrategy = .secondsSince1970
          decoder.keyDecodingStrategy = .convertFromSnakeCase
  
          var newsModel = [NewsModel]()
          
          do{
      
              
              let decodeData = try decoder.decode(NewsData.self, from: newsData)
           
              
              print(decodeData.articles.count)
              
              for article in decodeData.articles {
                  
              
                  let newsTitle = article.title
                  let newsDesc = article.description
                  let newsURL = article.url
                  let urlToImage = article.image
                  let publishedAt = article.publishedAt
                  let newsSource = article.source.name
                  
                      let data = NewsModel(title: newsTitle, decription: newsDesc, url: newsURL, image: urlToImage, publishedAt: publishedAt, sourcesName: newsSource)
                      
                          newsModel.append(data)
  
              }
              
              return newsModel
              
          } catch{
              print(&quot;Error decode the data : \(error.localizedDescription)&quot;)
      
          }
  
          return newsModel
      }
  }
  </code></pre>
  <p>**<strong>ViewController that will populate the data</strong></p>
  <p><strong>NewsCollectionVC.swift</strong>**</p>
  <pre><code>import UIKit
  import SafariServices
  
  class NewsCollectionVC: UIViewController {
      
  
      
      var newsManager = NewsManager()
      var newsResults = [NewsModel]()
      
      let iso8601Formatter = ISO8601DateFormatter()
      
      var dateFormatter : DateFormatter = {
          
          let formatter = DateFormatter()
          formatter.dateFormat = &quot;dd-MM-yyyy&quot;
          formatter.locale = Locale(identifier: &quot;en_US_POSIX&quot;)
          return formatter
      }()
      
      var pageNumber = 1
      var fetchMoreNews = false
      
      
      @IBOutlet weak var collectionView: UICollectionView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
          configureVC()
      }
      
      func configureVC(){
          
          collectionView.delegate = self
          collectionView.dataSource = self
          
          newsManager.delegate = self
          newsManager.fetchNews()
      }
      
  }
  
  
  extension NewsCollectionVC : UICollectionViewDataSource{
      
      
      // Tell delegate how many cv need to show
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          //return 2
          return newsResults.count
      }
      
      // Tell delegate content of cv
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          
          let listOfNews = newsResults[indexPath.row]
          
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;cell&quot;, for: indexPath) as! CViewCell
          
          cell.title.text = listOfNews.title
          cell.desc.text = listOfNews.decription
          cell.source.text = listOfNews.sourcesName
          
          // Convert damn ISO8601 to other format, https://developer.apple.com/forums/thread/660878
          if let date = iso8601Formatter.date(from: listOfNews.publishedAt){
              let formattedString = dateFormatter.string(from: date)
              cell.date.text = formattedString
          }
          
          cell.newsImage.downloaded(from: listOfNews.image)
          cell.newsImage.contentMode = .scaleAspectFill
          
          return cell
      }
      
  }
  
  extension NewsCollectionVC : UICollectionViewDelegate {
      
      
      // Function to fetch more news
      func fetchOtherNews(){
          
          fetchMoreNews = true
          print(&quot;Begin Fetch More News&quot;)
          
          
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
              
              print(&quot;Page number : \(self.pageNumber) | newResults : \(self.newsResults.count)&quot;)
              self.fetchMoreNews = false
              self.newsManager.fetchNews(page: self.pageNumber)
              
              self.collectionView.reloadData()
          }
      }
      
      
      // Tell delegate when user start scroll
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
          
          let offSetY = scrollView.contentOffset.y
          let contentHeight = scrollView.contentSize.height
          
          if offSetY &gt; contentHeight - scrollView.frame.height * 1 {
              
              if !fetchMoreNews{
                  pageNumber += 1
                  fetchOtherNews()
                  collectionView.reloadData()
              }
          }
      }
      
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          let selectedNews = newsResults[indexPath.row]
          // Provide VC to run url in app
          let SafariVC = SFSafariViewController(url: selectedNews.url)
          // Present the app
          present(SafariVC, animated: true, completion: nil)
          
      }
  }
  
  
  extension NewsCollectionVC : NewsManagerDelegate {
      
      func didSendNewsData(_ newsManager: NewsManager, with news: [NewsModel]) {
          
          DispatchQueue.main.async{
              
              self.newsResults.append(contentsOf: news)
              self.collectionView.reloadData()
              print(&quot;Current Page Number : \(self.pageNumber) | News Result :\(self.newsResults.count)&quot;)
              
          }
      }
  }
  </code></pre>