How to pass newly created object into a variable? #201

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

Originally created by @Guferos on GitHub (Feb 7, 2018).

Hi,

It's more question than an issue. I want to create a new object and then immediately assign it into my class variable. This is how I've been doing that:

class StatsController {
    
    private var currentSession : Session?
    
    init() {
        weak var weakSelf = self
        CoreStore.perform(
            asynchronous: { (transaction) -> Session in
                let session = transaction.create(Into<Session>())
                return session
            },
            success: { (transaction) in
                weakSelf?.currentSession = CoreStore.fetchExisting(transaction)
            },
            failure: { (error) in
                print("Failed to create new stats session")
            }
        )
    }
}

It works, however it feels like a lot of code. Is it a correct and only way of returning that object into the main context?

Originally created by @Guferos on GitHub (Feb 7, 2018). Hi, It's more question than an issue. I want to create a new object and then immediately assign it into my class variable. This is how I've been doing that: ``` class StatsController { private var currentSession : Session? init() { weak var weakSelf = self CoreStore.perform( asynchronous: { (transaction) -> Session in let session = transaction.create(Into<Session>()) return session }, success: { (transaction) in weakSelf?.currentSession = CoreStore.fetchExisting(transaction) }, failure: { (error) in print("Failed to create new stats session") } ) } } ``` It works, however it feels like a lot of code. Is it a correct and only way of returning that object into the main context?
adam added the question label 2025-12-29 15:26:33 +01:00
adam closed this issue 2025-12-29 15:26:33 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 8, 2018):

Yes, this is correct (although the success argument is not really transaction, it's the session instance). It looks verbose because you are doing two things here:

  • Creating an object in a safe transaction
  • Passing it safely to the main thread

You'll see this better if you trim all the syntax stuff:

CoreStore.perform(
    asynchronous: { $0.create(Into<Session>()) },
    success: { [weak self] in self?.currentSession = CoreStore.fetchExisting($0) },
    failure: { _ in print("Failed to create new stats session") }
)
@JohnEstropia commented on GitHub (Feb 8, 2018): Yes, this is correct (although the `success` argument is not really `transaction`, it's the `session` instance). It looks verbose because you are doing two things here: - Creating an object in a safe transaction - Passing it safely to the main thread You'll see this better if you trim all the syntax stuff: ```swift CoreStore.perform( asynchronous: { $0.create(Into<Session>()) }, success: { [weak self] in self?.currentSession = CoreStore.fetchExisting($0) }, failure: { _ in print("Failed to create new stats session") } ) ```
Author
Owner

@iby commented on GitHub (Jan 11, 2019):

Glad I've found this! I think documentation would benefit from including this case as it seems pretty ordinary? Passing objects safely and Updating objects kind of imply that, but it's not apparent that the object must be fetched outside the transaction first to be simply accessed. 🤷‍♂️

@iby commented on GitHub (Jan 11, 2019): Glad I've found this! I think documentation would benefit from including this case as it seems pretty ordinary? [Passing objects safely](https://github.com/JohnEstropia/CoreStore#passing-objects-safely) and [Updating objects](https://github.com/JohnEstropia/CoreStore#updating-objects) kind of imply that, but it's not apparent that the object must be fetched outside the transaction first to be simply accessed. 🤷‍♂️
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#201