title: swift realm insert array in background thread use in main
body: <p>I have an array of objects received via rest response, which I want to insert into realm db in a <strong>background</strong> thread and use in a uicollectionview in <strong>main</strong> thread.
  As soon as I receive response from rest, I call callback function and insert array into db in a background thread. 
  The problem when I am trying to access in main thread property of object being inserted in background I'm getting exception (<strong>see below</strong>) which I assume because of object not yet inserted </p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'RLMException', reason:
    'Realm accessed from incorrect thread.</p>
  </blockquote>
  
  <p>The model </p>
  
  <pre><code>class User : Object, Mappable {
      dynamic var firstName: String?
      dynamic var lastName: String?
  
      required convenience init?(map: Map){
          self.init()
      }
  
      func mapping(map: Map) {
          firstName &lt;- map["firstName"]
          lastName &lt;- map["lastName"]
      }
  }
  </code></pre>
  
  <p>Inserting in a background thread...</p>
  
  <pre><code>DispatchQueue.global().async {
    let realm = try! Realm()
    try! realm.write {
      realm.add(users)
    }
  }
  </code></pre>
  
  <p>Rendering in UI...</p>
  
  <pre><code>    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! UserViewCell
  
      let user = users[indexPath.row]
      cell.firstName.text = user.firstName
      cell.lastName.text = user.lastName
  }
  </code></pre>
  
  <p>Please note that exception occur either on accessing firstName or lastName.</p>
  
  <p>Please let me know what I'm doing wrong here</p>