'Realm accessed from incorrect thread' prepareForSegue 错误; Swift

'Realm accessed from incorrect thread' error in prepareForSegue; Swift

我做错了什么会导致 'Realm accessed from incorrect thread' 错误?

    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
        }

    }

有时我会进入下一个视图,有时则不会。

有没有办法保证每次访问该领域时都使用相同的线程?

我还做错了什么导致这个错误?


我很确定发生崩溃是因为您试图在主线程中调用的 ReadArticleViewController 的 viewDidLoad 方法中使用来自 "background accessed Realm" 的文章。您不能从后台访问 Realm 以在 prepareForSegue: 方法中选择一篇文章,然后在 ReadArticleViewController 的主线程中使用该文章。 你为什么要在后台传递文章?