Unsafe transaction changes not persisted to disk if save() called on context #242

Closed
opened 2025-12-29 15:27:19 +01:00 by adam · 5 comments
Owner

Originally created by @markst on GitHub (Nov 20, 2018).

I'm using a UnsafeDataTransaction in combination with ListMonitor which I display results in progress prior to committing saved changes.

My ListMonitor does not update with results unless I call:

try? transaction.unsafeContext().save()

However upon attempting to persist my changes such as:

transaction?.commit({ [weak self] (error) in
    if error == nil {
        self?.navigationController?.popViewController(animated: true)
    } else {
        ErrorHandler.showError(with: error?.localizedDescription ?? "Error Occured...")
    }
})

My changes are not correctly written to persisted store.

Originally created by @markst on GitHub (Nov 20, 2018). I'm using a `UnsafeDataTransaction` in combination with `ListMonitor` which I display results in progress prior to committing saved changes. My `ListMonitor` does not update with results unless I call: ```swift try? transaction.unsafeContext().save() ``` However upon attempting to persist my changes such as: ```swift transaction?.commit({ [weak self] (error) in if error == nil { self?.navigationController?.popViewController(animated: true) } else { ErrorHandler.showError(with: error?.localizedDescription ?? "Error Occured...") } }) ``` My changes are not correctly written to persisted store.
adam closed this issue 2025-12-29 15:27:19 +01:00
Author
Owner

@markst commented on GitHub (Nov 20, 2018):

If I force saving on the parent context:

try? transaction?.unsafeContext().parent?.save()

The changes are correctly persisted.

@markst commented on GitHub (Nov 20, 2018): If I force saving on the parent context: ```swift try? transaction?.unsafeContext().parent?.save() ``` The changes are correctly persisted.
Author
Owner

@JohnEstropia commented on GitHub (Nov 20, 2018):

How do you confirm that the changes are not persisted?
Do you get an error within the commit block?

@JohnEstropia commented on GitHub (Nov 20, 2018): How do you confirm that the changes are not persisted? Do you get an error within the `commit` block?
Author
Owner

@markst commented on GitHub (Nov 20, 2018):

No error. I perform a fetch later on in the app's lifetime & no results are returned.

It appears due to self.hasChanges guard returning early & never breaking into saving the parent:
40f458a09c/Sources/NSManagedObjectContext%2BTransaction.swift (L186)

@markst commented on GitHub (Nov 20, 2018): No error. I perform a fetch later on in the app's lifetime & no results are returned. It appears due to `self.hasChanges` guard returning early & never breaking into saving the parent: https://github.com/JohnEstropia/CoreStore/blob/40f458a09c0177d15ceab1561acaede2799c4f2d/Sources/NSManagedObjectContext%2BTransaction.swift#L186
Author
Owner

@markst commented on GitHub (Nov 30, 2018):

@JohnEstropia
On further investigation it seems I should be using flush(). #71
This produces the desired effect with commit() correctly persisting to disk.

Is it all good to simply destroy the transaction if I changes should not be persisted?

@markst commented on GitHub (Nov 30, 2018): @JohnEstropia On further investigation it seems I should be using `flush()`. #71 This produces the desired effect with `commit()` correctly persisting to disk. Is it all good to simply destroy the transaction if I changes should not be persisted?
Author
Owner

@JohnEstropia commented on GitHub (Nov 30, 2018):

@markst Yes, discarding the transaction will remove all unsaved changes.

@JohnEstropia commented on GitHub (Nov 30, 2018): @markst Yes, discarding the transaction will remove all unsaved changes.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#242