title: swift Realm in background thread then app is crash
body: <p>It seems! I have tried everything but I cant able to do. </p>
  
  <p>I am getting data from database in every 2 mint and post data in API.And my app stuck for some sec in every 2 mint. The reason is when I am getting data from the database that time app is stuck. I have to do in the background. </p>
  
  <p>This is my code is working but the app is Stuck </p>
  
  <pre><code> func getObjects(type: Object.Type) -&gt; Results&lt;Object&gt;? {
  
              //... *****************************
              //...code is working but app is stuck 
  
              return realm.objects(type)
          }
  
   if let list = realm.getObjects(type: AssignedCalendars.self)?.filter("isRemove == \(true) AND isSynced == \(false)").toArray(ofType: AssignedCalendars.self) {
                  if list.count &gt; 0 {
                      list.forEach({ (calendar) in
                          let calendar_json = [APIKey.event_id : calendar.event_id, APIKey.offlineId: calendar.offlineId, APIKey.calendar_id: calendar.calendar_id.toArray(ofType: Int.self)] as [String: Any]
                          removedCalendars.append(calendar_json)
                      })
                  }
                  offlineRemovedCalendars.append(contentsOf: list)
                  eventData[APIKey.calendar_removelist] = removedCalendars
              }
  </code></pre>
  
  <p>TRY 1 . TRY for  run code in the background I have to try to use dispatch queue </p>
  
  <pre><code>func getObjects(type: Object.Type) -&gt; Results&lt;Object&gt;? {
  
              //... *****************************
              //...code is working but app is stuck 
  
              return realm.objects(type)
          }
  
  
   DispatchQueue.main.async(execute: {
          if let list = realm.getObjects(type: AssignedCalendars.self)?.filter("isRemove == \(true) AND isSynced == \(false)").toArray(ofType: AssignedCalendars.self) {
  
              DispatchQueue.global().async {
              if list.count &gt; 0 {
                  list.forEach({ (calendar) in
                      let calendar_json = [APIKey.event_id : calendar.event_id, APIKey.offlineId: calendar.offlineId, APIKey.calendar_id: calendar.calendar_id.toArray(ofType: Int.self)] as [String: Any]
                      removedCalendars.append(calendar_json)
                  })
              }
              offlineRemovedCalendars.append(contentsOf: list)
              eventData[APIKey.calendar_removelist] = removedCalendars
  
            } //.. end bac que
          }
          })//... main que
  </code></pre>
  
  <p>TRY 2. use realm in background But i dont know how to use this method </p>
  
  <pre><code>extension Realm {
  
  
  
      func writeAsync&lt;T : ThreadConfined&gt;(obj: T, errorHandler: @escaping ((_ error : Swift.Error) -&gt; Void) = { _ in return }, block: @escaping ((Realm, T?) -&gt; Void)) {
          let wrappedObj = ThreadSafeReference(to: obj)
          let config = self.configuration
          DispatchQueue(label: "background").async {
              autoreleasepool {
                  do {
                      let realm = try Realm(configuration: config)
                      let obj = realm.resolve(wrappedObj)
  
                      try realm.write {
                          block(realm, obj)
                      }
                  }
                  catch {
                      errorHandler(error)
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>TRY 3. Found one solution but it's not working. it's not executed in a good way. IT's ADD Method, not a get </p>
  
  <pre><code>var usersRef: ThreadSafeReference&lt;Results&lt;User&gt;&gt;?
  DispatchQueue.global().async {
      autoreleasepool{
          let realm = try! Realm()
          try! realm.write {
              realm.add(users)
          }
          usersRef = ThreadSafeReference(to: users)
      }
  }
  </code></pre>