How to debug errors in Realm DB?

Asked

Viewed 96 times

1

How can I debug this error and know its origin:

lib

c++Abi.dylib: terminating with uncaught Exception of type Realm ::Incorrect threadexception: Realm accessed from incorrect thread.

2 answers

2


Usually this error occurs because you declare your Realm outside a thread and are using some property of it within a thread. Example

let realm = try! Realm()
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { 
                //não pode utilizar o realm declarado fora da GDC detro da GDC pois da erro de thread incorret
                let pessoa = realm.objects(Pessoa).first

            })
  • Vlw!!! I managed to solve but forgot to put here!!

1

I’ve never messed with Realmdb, but from the error message, it looks like you’re trying to access Realm from an unauthorized thread.

I searched the Internet and found this here:

Multi-threading with Realm

The only rule regarding the use of Realm through threads is that instances of Realm, Realmobject or Realmresult cannot be passed from one thread to another. When you want to access the same data from different threads, you should get a separate instance for each thread (through Realm.getInstance(this), for example) and access its objects through a Realmquery. Although the objects are different, they refer to the same data on the disk and it will be possible to read and write on them from any thread!

I believe you have an instance of Realm created in some thread (in the main, for example) and are trying to access the object by another thread.

Debug suggestion:

  1. Put two breakpoints in the code:
    1. On the line where you create the object [1]
    2. On the error line, where you access the object [2]
  2. Executes the project
  3. When Debugger intercepts the code at breakpoint [1], check in Debug Navigator (shortcut: Command + 6) which thread is being created
  4. Let the code continue and try to reproduce the error
  5. When Debugger intercepts the code at breakpoint [2], do the same thing the first time: look at Debug Navigator and see which thread that code is running on

Maybe you can find the problem out there!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.