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 18:23:13 +01:00
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:
varsomething:DataStack={// ...}()// This is a function CALL; runs once
varsomething: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
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @ghost on GitHub (Jan 21, 2017).
I took reference to the example code creating a datastack like so.
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.
@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: