ios

Error: realm accessed from incorrect thread

고고 2021. 11. 19. 14:56
realm accessed from incorrect thread,
cannot construct reference to unmanaged object, which can be passed across threads directly

 

안녕하세요 ◠‿◠ 고고입니다.

스택오버플로우를 보다보니

‘you should never try to use let realm = try! Realm() to create a singleton Realm instance and than try to access that everywhere from your app.’
let realm = try! Realm()

저거를 하나로 두고 여러 개의 함수에서 불렀던 게 문제였습니다.

 

‘The best way to use Realm in a thread-safe way is to create a new reference to your Realm using let realm = try! Realm() every time you move between threads and need to access your Realm. This way you can ensure that you will never get the incorrect thread exception.’

 

thread-safe하려면 매번 realm에 접근할 때마다 새로운 참조를 하는 게 가장 좋습니다. (매번 저 코드를 함수 안에 넣어주어야 함.)

 

 

스택오버플로우 : https://stackoverflow.com/questions/45375468/realm-thread-safe-object-with-singleton