Question about AddStorageAndWait. #123

Closed
opened 2025-12-29 15:24:56 +01:00 by adam · 1 comment
Owner

Originally created by @ghost on GitHub (Jan 21, 2017).

I took reference to the example code creating a datastack like so.

    struct Static {

   static let datastack: DataStack = {
   let datastack = DataStack(modelName: "Persistence")
   try! datastack.addStorageAndWait((SQLiteStore(fileName: "Persistence",     localStorageOptions: .recreateStoreOnModelMismatch)))
   return datastack
     }()
    }

However my concern is that I'm using Static.datastack everywhere to reference the datastack. Wouldn't it keep adding a Storage every single time I reference it somewhere. For example every time I would call Static.datastack.beginAsynchronous, which is called dozens of times in the lifetime of the app.

Originally created by @ghost on GitHub (Jan 21, 2017). I took reference to the example code creating a datastack like so. struct Static { static let datastack: DataStack = { let datastack = DataStack(modelName: "Persistence") try! datastack.addStorageAndWait((SQLiteStore(fileName: "Persistence", localStorageOptions: .recreateStoreOnModelMismatch))) return datastack }() } However my concern is that I'm using Static.datastack everywhere to reference the datastack. Wouldn't it keep adding a Storage every single time I reference it somewhere. For example every time I would call Static.datastack.beginAsynchronous, which is called dozens of times in the lifetime of the app.
adam added the question label 2025-12-29 15:24:56 +01:00
adam closed this issue 2025-12-29 15:24:56 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jan 22, 2017):

This syntax uses an inline closure call to initialize the stack, which gets called once the first time it is called. I think you are confusing it with computed variables. Note the difference in the syntax:

var something: DataStack = {
    // ...
}() // This is a function CALL; runs once
var something: DataStack {
    // ...
} // This is a function BODY; runs everytime
@JohnEstropia commented on GitHub (Jan 22, 2017): This syntax uses an inline closure call to initialize the stack, which gets called once the first time it is called. I think you are confusing it with computed variables. Note the difference in the syntax: ```swift var something: DataStack = { // ... }() // This is a function CALL; runs once ``` ```swift var something: DataStack { // ... } // This is a function BODY; runs everytime ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#123