title: Realm.io - Realm accessed from incorrect thread swift
body: <p>I want to download cities (name,id,lon,lat) from my .json file to Realm database.</p>
  
  <p>I have created Realm object called City, but it crashes with the following error: </p>
  
  <pre><code>Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread'** 
  </code></pre>
  
  <p>So, I just want to get a Realm objects in my <code>self.cities</code> (<code>[City]</code>) array. Then to put it in the table view.</p>
  
  <p>My refresh function:</p>
  
  <pre><code>func refresh(sender:AnyObject)
  {
  
          var service = CityService()
  
          service.getCities {
              (response) in
  
              if(response.count &gt; 0){
                  if(response["cities"] != nil){
                      self.citiesStore.loadCities(response["cities"]! as NSArray,tableView: self.tableView)
                      self.tableView.reloadData()
                  } else {
                      println("No response data")
                  }
              }
  
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>class CityService {
  var getCityUrl = "http://test:8888/cities.json";
  
  func getCities(callback:(NSDictionary)-&gt;()) {
      get(getCityUrl,callback:callback)
  }
  
  func get(url:String, callback:(NSDictionary) -&gt; ()){
      var nsURL = NSURL(string: url)
  
      UIApplication.sharedApplication().networkActivityIndicatorVisible = true
  
      let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!) {
          (data,response,error) in
          var error: NSError?
  
          UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  
          if(error == nil){
              if(response != nil){
                  var response = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &amp;error) as NSDictionary
                  callback(response)
              } else {
                  callback(NSDictionary())
              }
          }
  
      }
  
      task.resume()
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And loadCities function</p>
  
  <pre><code>func loadCities(citiesJson:NSArray,tableView: UITableView) {
  
      var realmQueue = dispatch_queue_create("db", DISPATCH_QUEUE_SERIAL)
  
      let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
      dispatch_async(realmQueue) {
          println("start")
  
          var cc:[City] = []
  
  
          var realm = RLMRealm.defaultRealm()
  
          realm.beginWriteTransaction()
  
          for object in citiesJson {
  
              City.createOrUpdateInRealm(realm, withObject: object)
  
          }
  
          realm.commitWriteTransaction()
  
          let citiesFromRealm = City.allObjects()
  
          for object in citiesFromRealm {
              let cityObject = object as City
  
              //println(cityObject.name)  &lt;-- here ok
  
              cc.append(cityObject)
          }
  
          println("point")
  
          dispatch_async(dispatch_get_main_queue()) {
              println("ok")
              self.cities.removeAll(keepCapacity: false)
              self.cities = cc
  
              println(self.cities[3].name) // &lt;--- Here an error
  
              tableView.reloadData()
          }
      }
  
  
  }
  </code></pre>