Skip to content

Transaction Changes Not Merged or Triggering Publishers #514

Description

@Devenias

I have an issue with transactions and publishers.
When I update existing models in a .perform block with it's transaction context transaction.fetchOne the data is not correct after execution, still has the old data and does not trigger the publisher. So my UI never noticed a change, because it did not happen.

When I use DatabaseStack.instance.dataStack.fetchOne instead, I can edit the object and it also triggers the data update on the publisher. I'm I doing something wrong or is that a bug, that the transaction context is not fully merged into the main context?

I'm using CoreStore 9.2.0 because I have to support iOS 15.

I get some new data and update in an async transaction:

DatabaseStack.instance.dataStack.perform { transaction in
    for data in newData {
        let fetch = From<MyModel>().where(\.id == data.id)
        
        // this does NOT trigger updates in the publisher
        let dbModel_1: MyModel = if
            let existing = try? transaction.fetchOne(fetch),
            let editable = transaction.edit(existing)
        {
            editable
        } else {
            transaction.create(Into(MyModel.self))
        }

        dbModel_1.updateWith(entity: data)

        // this does trigger updates in the publisher
        let dbModel_2: MyModel = if
            let existing = try? DatabaseStack.instance.dataStack.fetchOne(fetch)
        {
            existing
        } else {
            transaction.create(Into(MyModel.self))
        }
        
        dbModel_2.updateWith(entity: data)
    }
} completion: { result in
    ...
}

and I observe it like that:

let query = From<MyModel>().orderBy(.ascending(\.id))
return DatabaseStack.instance.dataStack
    .publishList(query)
    .reactive
    .snapshot(emitInitialValue: true)
    .tryMap { snapshot in
        Mapper.toEntities(snapshot)
    }
    .eraseToAnyPublisher()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions