I have some weird memory issues in an app and I'm wondering if I'm doing the correct thing here. I use Realm and have e.g:
try! self.realm.write {
self.realm.add(newItem)
}
But I'm wondering if I'm causing a retain cycle inadvertently so should maybe do:
try! self.realm.write { [unowned self] in
self.realm.add(newItem)
}
Which would be correct and why?
If you have a look at the write
method declaration, you will see, that the closure is non escaping. So, you don't need to use neither weak
nor unowned
. It will not lead to retain cycle.
public func write(_ block: (() throws -> Void)) throws {
beginWrite()
do {
try block()
} catch let error {
if isInWriteTransaction { cancelWrite() }
throw error
}
if isInWriteTransaction { try commitWrite() }
}