title: How to fetch data from Cloud Firestore to Xcode Uikit TableViewController?
body: <p>I try to display data from from cloud firestore in tableview. Before trying this my app worked fine. I have a tableview with basic cells image title and a subtitle in each row. Also a URL of video files which start streaming when user taps a cell. Now I want those cells to display data from Cloud Firestore. Appreciate any help. Thank you in advance.</p>
  <p>This is the swift file where everything worked before I started to try to list the data from cloud Firestore.</p>
  <pre><code>
  import UIKit
  import AVKit
  import AVFoundation
  import FirebaseFirestore
  import SwiftUI
  
  class ListOfVideoLessonsTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      
      
      let pinchGesture = UIPinchGestureRecognizer()
      
      @ObservedObject private var viewModel = VideosViewModel()
      var player = AVPlayer()
      var playerViewController = AVPlayerViewController()
     
   
      @IBOutlet var table: UITableView!
      
   
      override func viewDidLoad() {
          super.viewDidLoad()
          
          
          self.viewModel.fetchData()
  
          self.title = &quot;Video Lessons&quot;
          
          table.delegate = self
          table.dataSource = self
          // Do any additional setup after loading the view.
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          print(&quot;videos count = &quot;, viewModel.videos.count)
          return viewModel.videos.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: &quot;Cell&quot;, for: indexPath)
          let video = viewModel.videos[indexPath.row]
          tableView.tableFooterView = UIView()
          
          //configure cell
          cell.textLabel?.text = video.name
          cell.detailTextLabel?.text = video.lessonName
          cell.accessoryType = .disclosureIndicator
          let imageName = UIImage(named: video.imageName)
          cell.imageView?.image = imageName!.withRenderingMode(.alwaysOriginal)
          let backgroundView = UIView()
          backgroundView.backgroundColor = UIColor(named: &quot;VideoLessonsCellHighlighted&quot;)
          cell.selectedBackgroundView = backgroundView
          cell.textLabel?.font = UIFont(name: &quot;Helvetica-Bold&quot;, size: 14)
          cell.detailTextLabel?.font = UIFont(name: &quot;Helvetica&quot;, size: 12)
          
          return cell
      }
      
  
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
          return 100
          
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          tableView.deselectRow(at: indexPath, animated: true)
          
          
          playVideo(at: indexPath)
          
      }
      
       func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
          if let cell = tableView.cellForRow(at: indexPath) {
              cell.contentView.backgroundColor = UIColor(named: &quot;VideoLessonsCellHighlighted&quot;)
              cell.textLabel?.highlightedTextColor = UIColor(named: &quot;textHighlighted&quot;)
              cell.detailTextLabel?.highlightedTextColor = UIColor(named: &quot;textHighlighted&quot;)
              
  
              }
  
      }
  
       func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
          if let cell = tableView.cellForRow(at: indexPath) {
              cell.contentView.backgroundColor = nil
              
              }
  
      }
  
          func playVideo(at indexPath: IndexPath){
              let selectedVideo = viewModel.videos[indexPath.row]
              let url = URL(string: selectedVideo.fileURL)
              player = AVPlayer(url: url!)
              playerViewController.player = player
              
              
            self.present(playerViewController, animated: true, completion: {
                  self.playerViewController.player?.play()
              })
          }
  }
  
  </code></pre>
  <p>I have a VideoViewModel.swift file</p>
  <pre><code>import Foundation
  import FirebaseAnalytics
  import FirebaseFirestore
  
  class VideosViewModel: ObservableObject {
      @Published var videos = [Video]()
      
      private var db = Firestore.firestore()
      
     func fetchData() {
          db.collection(&quot;videos&quot;).addSnapshotListener { [self] (querySnapshot, error) in
              guard let documents = querySnapshot?.documents else {
                  print(&quot;No Documents&quot;)
                  return
              }
              
              self.videos = documents.map { (queryDocumentSnapshot) -&gt; Video in
                 let data = queryDocumentSnapshot.data()
                  
                  let name = data[&quot;name&quot;] as? String ?? &quot;&quot;
                  let imageName = data[&quot;imageName&quot;] as? String ?? &quot;&quot;
                  let lessonName  = data[&quot;lessonName&quot;] as? String ?? &quot;&quot;
                  let fileURL  = data[&quot;fileURL&quot;] as? String ?? &quot;&quot;
          
                  
                  print(data)
  
  
                  return Video(name: name, imageName: imageName, lessonName: lessonName, fileURL: fileURL)
                  
                
      
              }
          }
      }
  }
  
  </code></pre>
  <p>When I print print(&quot;videos count = &quot;, viewModel.videos.count) It shows 000 videos,</p>
  <pre><code>videos count =  0
  videos count =  0
  videos count =  0
  
  </code></pre>
  <p>When I print  print(data) it shows data from firebase firestore.</p>
  <pre><code>[&quot;lessonName&quot;: Lesson Name, &quot;name&quot;: Name, &quot;fileURL&quot;: gs://tff-sample.appspot.com/Video Lessons/mixkit-hands-of-a-man-playing-on-a-computer-43527.mp4, &quot;imageName&quot;: gs://tff-sample.appspot.com/Video Images/1024x1024.png]
  [&quot;lessonName&quot;: LessonName, &quot;name&quot;: Name 2, &quot;fileURL&quot;: gs://tff-sample.appspot.com/Video Lessons/mixkit-meadow-surrounded-by-trees-on-a-sunny-afternoon-40647., &quot;imageName&quot;: gs://tff-sample.appspot.com/Video Images/1024x1024.png]
  </code></pre>