title: Fetching Core Data object in the background and tableView.reloadData()
body: <p>I want to load Core Data objects in the background, and when it´s finished, should reload a TableView. The code below gives an empty tableView: </p>
  
  <pre><code>class ViewController: UIViewController {
   var fetchRequest: NSFetchRequest&lt;Venue&gt;!
   var venue:[Venue] = []
   var coreDataStack: CoreDataStack!
  
   override func viewDidLoad() {
        super.viewDidLoad()
        fetchRequest = Venue.fetchRequest()
        coreDataStack.storeContainer.performBackgroundTask {(context) in
             do {
                  self.venue = try context.fetch(self.fetchRequesrt)
             } catch {
             }
             DispatchQueue.main.async {
                  self.tableView.reloadData()
             }
        }
  }
  </code></pre>
  
  <p>But if we write code in viewDidLoad() without chunk above, all works fine and tableView is populated with data: </p>
  
  <pre><code>venue = try! coreDataStack.managedContext.fetch(fetchRequesrt)
  tableView.reloadData()
  </code></pre>
  
  <p>What is wrong with the code above?
  For advices I would be very grateful!</p>
  
  <p>Edit. That's the right solution:</p>
  
  <pre><code>class ViewController: UIViewController {
   var fetchRequest: NSFetchRequest&lt;Venue&gt;!
   var venue:[Venue] = []
   var coreDataStack: CoreDataStack!
  
   override func viewDidLoad() {
     super.viewDidLoad()
     fetchRequest = Venue.fetchRequest()
     coreDataStack.storeContainer.performBackgroundTask {(context) in
     do {
      let venue = try context.fetch(self.fetchRequesrt)
      venue.map {$0.objectID}.forEach{self.venue.append(self.coreDataStack.managedContext.object(with: $0) as! Venue)}
      DispatchQueue.main.async {
       self.tableView.reloadData()
      }
     } catch {
     ...
     }
    }
    }
  }
  </code></pre>
  
  <p>One more solution to use NSAsynchronousFetchRequest class. The example above would look like this:</p>
  
  <pre><code>class ViewController: UIViewController {
    var fetchRequesrt: NSFetchRequest&lt;Venue&gt;!
    var venue:[Venue] = []
    var asyncFetchRequest: NSAsynchronousFetchRequest&lt;Venue&gt;!
    var coreDataStack: CoreDataStack!
    fetchRequest = Venue.fetchRequest()
    override func viewDidLoad() {
      super.viewDidLoad()
     asyncFetchRequest = NSAsynchronousFetchRequest(fetchRequest:fetchRequest, completionBlock: { [unowned self] 
     (result: NSAsynchronousFetchResult) in
    guard let venue = result.finalResult else { return }
     self.venue = venue
     self.tableView.reloadData()
     })
     do {
       try coreDataStack.managedContext.execute(asyncFetchRequest)
       } catch {
        ...
       }
      }
     }
  </code></pre>