For Realm Swift, Could I Use Dispatchqueue with Autorelease Frequency of .Workitem Instead of Autoreleasepool

Does every custom GCD queue need an autorelease pool under ARC?

It depends on your usage of autoreleased objects.

Every GCD thread has an outermost autorelease pool, but that pool is drained at a time you cannot control directly (currently the drain occurs when the thread becomes idle, just before it parks itself in the kernel awaiting reuse or reaping).

If your process keeps GCD threads active for long periods of time and/or if you create a large number of autoreleased objects (or very large autoreleased objects) in your blocks, you may wish to create a pool in your blocks to ensure the resources occupied by those objects are freed up earlier.

Unable to determine possible causes of EXC_BAD_ACCESS (which occurs during app startup)

In the emptyList method you have this:

let realm = try! Realm(configuration: configuration!)

This is going to crash (the try! will bug out) if the realm cannot be created. This will happen if resources are constrained, which chimes with you only seeing it when memory is low.

See https://realm.io/docs/swift/latest/#error-handling for proper error handling (basically it's a do-catch).

I would add checks for all the things in @Augie's answer too.

Realm - database isn't up to date after a delete

Make sure you put the async block in an autorelease pool:

 myDeleteFunc(completion: {
DispatchQueue.main.async {
let realm = try! Realm()
let objects = Array(realm.objects(MyObject.self).sorted(byProperty: "aProperty"))
// Here objects still contains the object that I have already deleted
// ...
}
}

Should be

 myDeleteFunc(completion: {
DispatchQueue.main.async {
autoreleasepool {
let realm = try! Realm()
let objects = Array(realm.objects(MyObject.self).sorted(byProperty: "aProperty"))
// Here objects still contains the object that I have already deleted
// ...
}
}
}

Make sure you do this autoreleasepool { ... } wrap for any background thread where you create a Realm instance, primarily in the GCD.


If that still doesn't work, you can do:

 myDeleteFunc(completion: {
DispatchQueue.main.async {
autoreleasepool {
let realm = try! Realm()
realm.refresh()


Related Topics



Leave a reply



Submit