[Question] is Async Transaction is thread safe? #315

Closed
opened 2025-12-29 15:28:42 +01:00 by adam · 2 comments
Owner

Originally created by @seanliu1 on GitHub (Feb 19, 2020).

I have a question regarding perform async function, I looked into the source code, but still not very clear for me. Here are code samples.

  group.enter()
  dataStack.perform(asynchronous: {(transaction) in 
        var p = transaction.create(Into<Person>())
        p.id = 1
        p.name = "test"
   }, completion {
        group.leave() 
  }

  group.enter()
   dataStack.perform(asynchronous: {(transaction) in 
        var p = try transaction.fetchOne(From<Person>().where(\Person.id == Int64(1))
        // can we guarantee we can read P from the previous insertion 
   }, completion {
        group.leave() 
  }

in the second group.enter, if we fetchOne, can we get the object we just inserted.

Originally created by @seanliu1 on GitHub (Feb 19, 2020). I have a question regarding perform async function, I looked into the source code, but still not very clear for me. Here are code samples. ``` group.enter() dataStack.perform(asynchronous: {(transaction) in var p = transaction.create(Into<Person>()) p.id = 1 p.name = "test" }, completion { group.leave() } group.enter() dataStack.perform(asynchronous: {(transaction) in var p = try transaction.fetchOne(From<Person>().where(\Person.id == Int64(1)) // can we guarantee we can read P from the previous insertion }, completion { group.leave() } ``` in the second group.enter, if we fetchOne, can we get the object we just inserted.
adam added the question label 2025-12-29 15:28:42 +01:00
adam closed this issue 2025-12-29 15:28:43 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 20, 2020):

No, you can't. As the method name suggests, that is an asynchronous transaction. In fact, transactions run a serial asynchronous queue, so you don't really need your DispatchGroup there.

You can fetch the first created Person from inside the first transaction's completion, and also from inside the second transaction's asynchronous closure.

@JohnEstropia commented on GitHub (Feb 20, 2020): No, you can't. As the method name suggests, that is an asynchronous transaction. In fact, transactions run a serial asynchronous queue, so you don't really need your `DispatchGroup` there. You can fetch the first created `Person` from inside the first transaction's `completion`, and also from inside the second transaction's `asynchronous` closure.
Author
Owner

@seanliu1 commented on GitHub (Feb 20, 2020):

Thanks, sorry I mean in the second transaction's async closure.

@seanliu1 commented on GitHub (Feb 20, 2020): Thanks, sorry I mean in the second transaction's async closure.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#315