Periodically in my app, I need to perform large writes to Realm, anywhere between 100 to 10,000 objects. Obviously this is a large write, so I'm attempting to perform this write in the background so that the user can perform other operations and not even notice the write. Unfortunately, even though I thought my write was being performed on a background thread, the main UI still gets blocked. Here is the jist of the method that I call to perform the writes to realm. This method is called repeatedly on single objects from an array I'm looping through. Does it look like I'm doing anything blatantly wrong? Any help would be greatly appreciated.
func writeCustomerToRealm(inputCustomer:Customer) {
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let realm = try! Realm()
realm.beginWrite()
realm.add(self.swapCustomerForRealmCustomer(inputCustomer))
try! realm.commitWrite()
})
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With