How to ensure that some code runs on same background thread with runloop

310 Views Asked by At

I am using realm in an iOS app.

I am calling realm methods in a background thread. Realm objects can be used only in the thread where it was created.

This means that I have to ensure that all the code related to accessing realm objects happen on the same thread. This question has been answered here - How to ensure to run some code on same background thread?

The background thread has no runloop and hence realm objects do not get refreshed automatically.

Can any one point out how to create a background thread with a runloop so that the code blocks can be executed on the same background thread with runloop?

1

There are 1 best solutions below

1
Andrew Lombard On

Not knowing more about your use case (or Realm for that matter!) a suggestion - do you need to use a background thread at all?

Seems that some suggest the Main UI Thread is fine most of the time and may simplify your life.

Is it acceptable to load Realm objects in the main ui thread?