Skip to main content
 首页 » 数据库

ios之prepareForSegue 中出现“从不正确的线程访问的 Realm ”错误; swift

2024年01月01日73haluo1

我做错了什么会导致“从不正确的线程访问 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 
        } 
 
    } 

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

有没有办法保证将使用哪个线程,以便我可以确保每次访问该 Realm 时都使用相同的线程?

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

请您参考如下方法:


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