Guidance on how to use SQLite data in a manager class #263
BennoCrafter
started this conversation in
General
Replies: 2 comments 9 replies
-
|
Hi @BennoCrafter, I'm not sure what you mean by "manager class", so any sample code you can share will be helpful. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
hi! for sure. So this is my main entry @main
struct TimetableApp: App {
// @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Environment(\.scenePhase) private var scenePhase
@AppStorage(AppStorageKeys.hasSeenOnboarding.keyName()) private var hasSeenOnboarding: Bool = false
@StateObject var alertManager: AlertManager = .init()
@Dependency(\.context) var context
@ObservedObject var timetableManager = TimetableManager.shared
@Dependency(\.defaultDatabase) private var database
init() {
if context == .live {
try! prepareDependencies {
try $0.bootstrapDatabase()
}
}
timetableManager.load()
}
And a simplified version of TimetableManager (which is the manager class im talking about): class TimetableManager: ObservableObject {
static let shared = TimetableManager()
@ObservationIgnored
@FetchAll var events: [Event]
@Published var appConfig = AppConfig()
@ObservationIgnored @Dependency(\.defaultDatabase) private var database
private let logger = Logger(subsystem: "TimetableManager", category: "Database")
private init() {}
func load() {}
func appDatabase() throws -> any DatabaseWriter {
var configuration = Configuration()
let database = try defaultDatabase(configuration: configuration)
logger.info("Open Database: '\(database.path)'")
var migrator = DatabaseMigrator()
#if DEBUG
migrator.eraseDatabaseOnSchemaChange = true
#endif
migrator.registerMigration("Create initial tables") { db in
let defaultEventColor = Color.HexRepresentation(queryOutput: Event.defaultColor).hexValue
let defaultTemplateColor = Color.HexRepresentation(queryOutput: EventTemplate.defaultColor).hexValue
// MARK: - Events
try #sql(
"""
CREATE TABLE "events" (
"id" TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE DEFAULT (uuid()),
"name" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT 'UNKNOWN',
"color" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT \(raw: defaultEventColor ?? 0),
"startDate" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT (date('1970-01-01')),
"endDate" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT (date('9999-12-31')),
"useDateOnly" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0,
"note" TEXT,
"occurrenceId" TEXT REFERENCES "occurrences"("id") ON DELETE SET NULL,
"templateId" TEXT REFERENCES "eventTemplates"("id") ON DELETE SET NULL,
"isLocked" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0
) STRICT
"""
).execute(db)
}
try migrator.migrate(database)
return database
}
func addEvent(_ event: Event) {
// insert into table
}
Simply said what. I mean by a manager class. It's an Environment object which I access in my different views and can perform different IO tasks to it. E.g. simplified adding an item to a table. |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, so I’m currently trying to figure out how to integrate this library into a Manager class. When I tried the most obvious approach, initialising it in the main app’s initialisation and having an observable singleton class, I got an error saying I can’t initialise it a second time. Any guidance would be great, or is the Manager class the wrong approach here?
Beta Was this translation helpful? Give feedback.
All reactions