从不正确的线程访问领域 - 分派异步

时间:2016-03-19 20:56:14

标签: swift multithreading realm

我试图在不同的线程上进行一些排序,但是我不断获得Realm accessed from incorrect thread。以下是我到目前为止所尝试的内容。

        let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
        dispatch_async(dispatch_get_global_queue(priority, 0)) {
            // do some task
            self.organizationArray = GetOrganization.sortOrganizationsByDistanceFromLocation(realm.objects(Organization), location: self.lastLocation!)
            dispatch_async(dispatch_get_main_queue()) {
                self.lastLoadedPage = self.lastLoadedPage + 1

                if numberOfResults < self.limit {
                    //Hide FooterView
                    self.moreDataAvailable = false
                    self.hideTableViewFooter()

                }
            }
        }

1 个答案:

答案 0 :(得分:0)

假设您正在从主线程中调用显示的代码段,例如从视图控制器看来,你很可能在另一个线程的主线程中使用RLMRealm实例。

 let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
        dispatch_async(dispatch_get_global_queue(priority, 0)) {
            // Get a Realm for the current thread
            RLMRealm *realm = [RLMRealm defaultRealm];

            // do some task with that instance instead
            self.organizationArray = GetOrganization.sortOrganizationsByDistanceFromLocation(realm.objects(Organization), location: self.lastLocation!)
            // …