在prepareForSegue中'从错误的线程访问的域'错误;迅速

时间:2015-03-14 12:05:14

标签: ios multithreading swift grand-central-dispatch realm

我做错了什么会导致'从错误的线程访问的域'错误?

    2015-03-14 06:59:54.962 MyApp[25646:1218284] *** Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread'
*** First throw call stack:
(
    0   CoreFoundation                      0x01961466 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01212a97 objc_exception_throw + 44
    2   MyApp                         0x00120329 _ZL11RLMGetArrayP13RLMObjectBasejP8NSString + 382
    3   MyApp                         0x00122952 ___ZL17RLMAccessorGetterP11RLMPropertycP8NSString_block_invoke291 + 25
    4   MyApp                         0x00047f87 _TFFC11 MyApp 25ReadArticleViewController11viewDidLoadFS0_FT_T_U_FT_T_ + 823
    5   MyApp                         0x0003da44 _TPA__TFFC11 MyApp 25ReadArticleViewController11viewDidLoadFS0_FT_T_U_FT_T_ + 52
    6   MyApp                         0x0004bce8 _TTRXFo__dT__XFdCb__dT__ + 40
    7   libdispatch.dylib                   0x0384eaf8 _dispatch_block_async_invoke_and_release + 347
    8   libdispatch.dylib                   0x0386dbef _dispatch_client_callout + 14
    9   libdispatch.dylib                   0x038551ef _dispatch_root_queue_drain + 1092
    10  libdispatch.dylib                   0x03856b70 _dispatch_worker_thread3 + 115
    11  libsystem_pthread.dylib             0x03bc91da _pthread_wqthread + 724
    12  libsystem_pthread.dylib             0x03bc6e2e start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我的prepareForSegue?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

        if segue.identifier == "readArticle" {

            let indexPathRow = tableView.indexPathForSelectedRow()!.row// because I need to show a progress view whie the next view loads // sometimes it works.

            Async.background {
                println("BEFORE")
                var Realm = RLMRealm.defaultRealm()
                if let article = Article.allObjects()[UInt(indexPathRow)] as? Article
                {
                    var destination = segue.destinationViewController as ReadArticleViewController
                    destination.article = article as Article
//                  destination.realmQueue = self.realmQueue as dispatch_queue_attr_t
                }
                println("AFTER")

            }.main {
                println("finished segue")
            }
        }
        else if segue.identifier == "newArticle" {
//          let newArticleViewController = segue.destinationViewController as NewArticleViewController
        }

    }

有时我会将它转到下一个视图,有时候不会。

无论如何都要保证使用哪个线程,这样我每次访问域时都可以确保使用相同的线程?

我还有什么不对,导致此错误?

1 个答案:

答案 0 :(得分:3)


我非常确定崩溃的发生是因为您正在尝试使用来自后台访问的Realm"在ReadArticleViewController的viewDidLoad方法中,该方法在主线程中调用。您无法从后台访问Realm以在prepareForSegue:方法中选择一篇文章,然后在ReadArticleViewController的主线程中使用该文章。 你为什么试图在背景中传递这篇文章呢?