title: "Realm accessed from incorrect thread" exception thrown from dispatch_group_notify block
body: <p>I have read the other answers but couldn't find a suitable solution.</p>
  
  <p>I have a product which is uploaded to server only if all IMAGES belonging to that product have finished uploading. The product's details (along with the images) are filled on view controller 1 and then he is taken to the next screen (view controller 2), regardless of whether all images have finished uploading or not. MY VC1 completes the product upload process like this.</p>
  
  <pre><code>let areAllImagesUploaded = RetailProductsService.sharedInstance.checkProductImageDependency(realm!, uuid: retailProduct.getClientId())
  
  if areAllImagesUploaded {
      uploadProductToServer(retailProduct)
  } else {
      do {
          try realm = Realm()
          RetailProductsService.sharedInstance.updateSyncStatusForSellerProduct(realm!, clientId: retailProduct.getClientId(), syncStatus: ProductSyncStatus.SYNC_FAILED)
          let groupT = dispatch_group_create()
  
          for sellerSKU in retailProduct.sellersSKUs {
              for productImage in sellerSKU.productImages { 
                  dispatch_group_enter(groupT)
                  let imageUploadInfo = ImageUploadInfo(productId: retailProduct.getClientId(), imageId: sellerSKU.getId(),imageData: productImage.imageData, uploadURL: ServerConfig.RETAIL_SERVER_UPLOAD_FILE_URL)
                  ImageUploadManager.sharedInstance.queueImageForUpload(imageUploadInfo, completion: { (success, error) -&gt; Void in
                      dispatch_group_leave(groupT)
                  })
  
                  dispatch_group_notify(groupT, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -&gt; Void in
                      //self.uploadProduct(retailProduct)
                      self.uploadProductToServer(retailProduct) // Fails here
                  })
  
              }
          }
      } catch {
          print("Error in saving product.")
      }
  }
  </code></pre>
  
  <p>I have marked the line on which I get this error. My app has moved to the next view controller while this function in view controller 1 continues uploading images and as soon as all images associated the product are uploaded to server, it tries to upload the product. However it fails with this exception.</p>
  
  <pre><code>Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread'
  </code></pre>
  
  <p>Please help!</p>