Are main context object properties automatically updated from transactions? #347

Closed
opened 2025-12-29 18:25:55 +01:00 by adam · 2 comments
Owner

Originally created by @zachwaugh on GitHub (Oct 28, 2020).

In general, are properties of the read-only main context objects live updated? Assuming they are fetched initially from the main data stack and only accessed from the main thread with all changes done in transactions. Sorry if this is an obvious question, but I couldn't find a clear answer. Here's a simple example:

class Controller {
  // An individual message fetched from the main data stack
  let message = dataStack.fetchOne(From<Message>().where(\.id == 1))

  func update() {
    dataStack.perform(
      asynchronous: { transaction in
          // a property is updated in a transaction
          let message = transaction.edit(self.message)
          message.title = "Test"
      },
      completion: { _ in 
        self.didUpdate()
      }
    )
  }

  func didUpdate() {
    // This is after the transaction saved
    // will message.title == "Test"?
  }
}

From a quick test, it appears that the main dataStack properties are updated, but this comment indicates that would only be the case if there was an observer/monitor attached: https://github.com/JohnEstropia/CoreStore/issues/352#issuecomment-562797703. Hoping you can clear that up, thank you!

Originally created by @zachwaugh on GitHub (Oct 28, 2020). In general, are properties of the read-only main context objects live updated? Assuming they are fetched initially from the main data stack and only accessed from the main thread with all changes done in transactions. Sorry if this is an obvious question, but I couldn't find a clear answer. Here's a simple example: ```swift class Controller { // An individual message fetched from the main data stack let message = dataStack.fetchOne(From<Message>().where(\.id == 1)) func update() { dataStack.perform( asynchronous: { transaction in // a property is updated in a transaction let message = transaction.edit(self.message) message.title = "Test" }, completion: { _ in self.didUpdate() } ) } func didUpdate() { // This is after the transaction saved // will message.title == "Test"? } } ``` From a quick test, it appears that the main dataStack properties are updated, but this comment indicates that would only be the case if there was an observer/monitor attached: https://github.com/JohnEstropia/CoreStore/issues/352#issuecomment-562797703. Hoping you can clear that up, thank you!
adam closed this issue 2025-12-29 18:25:55 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Oct 29, 2020):

Objects in the main thread would get updated, eventually. What I meant in the comment you linked is that if you rely on the timing of the updates you should be using one of the Observers/Publishers otherwise you have no control when they get updated

@JohnEstropia commented on GitHub (Oct 29, 2020): Objects in the main thread would get updated, eventually. What I meant in the comment you linked is that if you rely on the timing of the updates you should be using one of the Observers/Publishers otherwise you have no control when they get updated
Author
Owner

@zachwaugh commented on GitHub (Oct 29, 2020):

Perfect, thank you!

@zachwaugh commented on GitHub (Oct 29, 2020): Perfect, thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#347