Realm write performed on background thread still blocks Main UI Realm write performed on background thread still blocks Main UI multithreading multithreading

Realm write performed on background thread still blocks Main UI


Your provided code is the correct way to perform an asynchronous, background write using Realm. It should not block the main thread unless the main thread itself attempts to perform a write transaction while the background write is in progress.

The instruments trace you shared indicates that the majority of the non-idle time spent on the main thread is spent processing web socket messages. There's no indication of any work related to Realm occurring on the main thread. The web socket message processing on the main thread accounts for 45% of the total time period of the trace, and is likely the cause of the blocked UI you're experiencing. The majority of the web socket processing time is spent performing work related to NSDateFormatter. In particular, the NSDateFormatter appears to be frequently regenerating its underlying CFDateFormatter. This suggests that properties of the NSDateFormatter are being frequently updated, or that new NSDateFormatter instances are being used for each call. Both of those practices should be avoided.