I'm getting a "RUNNINGBOARD 0xdead10cc" error intermittently when my app is in the background and I'm using Realm database. I suspect the error is related to file locking during database writes. I have tried adding sleep to the write block, but the error has not occurred during testing.
Here's a code snippet of the function I'm using to update my Realm database:
func update(completion: @escaping () -> ()) {
let dispatchGroup = DispatchGroup()
array1 = Array(Set(self.arrayData))
for element in array1 {
dispatchGroup.enter()
element.updateRealmModel {
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
completion()
}
}
and the element function:
func updateRealmModel(completion: @escaping(() -> Void)) {
let primaryKey = self.id
DispatchQueue.global(qos: .userInitiated).async {
let realm = try! Realm()
if let user = realm.object(ofType: Mytype, forPrimaryKey: primaryKey) {
do {
try realm.write {
user.boolField = false
}
} catch let error {
debugPrint(error)
}
}
completion()
}
}
Can anyone help me reproduce and resolve this error? Are there any known issues related to Realm database writes when an app is in the background? I would appreciate any insights or suggestions on how to debug and fix this issue.
I don't believe the RUNNINGBOARD error is directly related to Realm in this use case.
It doesn't appear those DispatchQueues are needed and in some cases, can be incredibly hard to manage - along with the completion handler, whoaaa.
Since the primary key of each user to be updated is known, there's no reason to query for the user - you already know who they are by their primary key.
It appears to be a small amount of data and if he array is reasonably sized, my first suggestion is to simply update each user object directly within the loop via their primary key.
The above will update the boolField property to false on each user with a primary key found in array1, and not modify any other properties.
That being said, if it's a large loop, that can tie up the UI so you could also dump the whole process onto a single background thread
which is a technique we use when the dataset is huge.
Another option is to leverage the
writeAsyncfunction to update objects asynchronouslyA nice explanation and example code can be found here Update Objects Asynchronously