New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RLMRealm instance was deallocated during a write transaction #3302
Comments
Yeah, this looks like your |
The posted code does not appear to be safe in the face of errors. It will leave the transaction uncommitted in that case. Using |
Oh, absolutely! How could I have missed that. Mark is right on that. 👍 |
@bdash I'm sorry to re-open this issue; but I'm failing to see where the transaction would be left uncomitted... 😶 Or is a (possibly) failing Also; say (in the above context) I wanted to save/update multiple objects (~100), is it more efficient to do this in |
public func write(_ block: (() throws -> Void)) throws {
beginWrite()
do {
try block()
} catch let error {
if isInWriteTransaction { cancelWrite() }
throw error
}
if isInWriteTransaction { try commitWrite() }
} The relevant portion is the call to If you're going to update multiple objects, it's preferable to do so within a single transaction. Whether you create the transaction using |
Ah I see, thanks for elaborating on this! I think I understand it a little more now (I just started with Realm a few hours ago). So if I'm following you right: // Assuming a realm was created in the current thread and the write won't fail
try! realm.write {
// #1 One can perform updates/insertions here? E.g.
realm.add(someThing, update: true)
realm.add(someOtherThing, update: true)
// #2 What about 'for'-loops here?
for thing in allTheThings {
realm.add(thing, update: true)
}
} In the above context; would that write transaction follow through correctly?
|
I'm not sure quite what you're asking there. The code you've provided looks sensible though. |
Ah, never mind. I think I got it now, I misunderstood the write block. |
So my realm has has been crashing randomly and i've been having trouble tracking it down. My device wasn't leaving crash logs but when running attached to Xcode i finally got this error in the console:
So as far as I can tell i'm using the following code block - but i'm confused as to why this can happen:
It "looks" like i'm just creating a realm on a background queue INSIDE of an autorelease block - so maybe its not here? Is there an easy way to track this error down?
The text was updated successfully, but these errors were encountered: