How to debug errors in Realm DB?

1

How can I debug this error and know its source:

lib

  

c ++ abi.dylib: terminating with uncaught exception of type realm :: IncorrectThreadException:

    
asked by anonymous 26.05.2016 / 17:40

2 answers

2

Generally this error occurs because you declare your realm outside a thread and you are using some property of it inside 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

            })
    
05.07.2016 / 20:12
1

I've never messed with RealmDB, but, through the error message, it looks like you're trying to access Realm by an unauthorized thread.

I searched the Internet and found it from here:

  

Multi-threading with Realm

     

The only rule about using Realm through threads is that instances of Realm, RealmObject, or RealmResult can not 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 your objects through a RealmQuery. Although the objects are different, they refer to the same data on the disk and you can read and write to them from any thread!

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

Debug suggestion:

  • Places two breakpoints in the code:
  • In the line where you create the object [1]
  • In the error line, where you access the object [2]
  • Run the project
  • When the debugger intercepts the code in breakpoint [1], check in the Debug Navigator (shortcut: Command + 6) which thread the object is being created in.
  • Let the code continue and try to reproduce the error
  • When the debugger intercepts the code in breakpoint [2], it does the same thing the first time: look at the Debug Navigator and check in which thread that code is running
  • Maybe you can find the problem out there!

        
    30.05.2016 / 17:58