marcoarment / Blackbird

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

README - Shouldn't RootView var database be a @StateObject?

jackpal opened this issue · comments

The Readme has this code:

struct RootView: View {
    // The database that all child views will automatically use
    var database = try! Blackbird.Database.inMemoryDatabase()
   ...

But I think that has the potential issue of the database being recreated if/when RootView is recreated, which SwiftUI can do at whim.

I think the @StateObject decorator is supposed to be used:

struct RootView: View {
    // The database that all child views will automatically use
    @StateObject
    var database = try! Blackbird.Database.inMemoryDatabase()
   ...

I notice that your actual example SwiftUI code doesn't have this issue because it stores the database in an App object rather than in a view.

You're right! Thanks. Fixed.