📦 SwiftyLaunch Modules
🗄️ DatabaseKit
Overview

DatabaseKit (DBKit) Module Overview

SwiftyLaunch Module - DatabaseKit

Powered by Firebase Firestore

DBKit wraps Firebase Firestore SDK to provide direct examples on how to interact with the database in your app.

DBKit is bundled together with AuthKit and is referenced to as FirebaseKit within the apps generated via SwiftyLaunch.

After generating a project with DatabaseKit enabled and completing the initial project setup, you will get following features out of the box:

  • An extensive database usage example: Example of fetching, creating, deleting infromation from the backend. Read more.
  • (AnalyticsKit only) Automatic analytics tracking: Automatically track events related to paywalls and purchases. Read more.
  • (BackendKit only) BackendKit integration: You can also access your database on the backend. Read more.

Module Deep Dive

DBKit uses the environment object class DB to interact with the Database. This object is attached to the root view and is accessible to all other views within the app (except for modules that don't have FirebaseKit selected as a module. Click here to read how modules work)

The environment object DB is also utilized by AuthKit and BackendKit to utilize other Firebase-related operations (accessing FirebaseAuth in AuthKit, and Cloud Functions in BackendKit).

FirebaseBackend.swift
@MainActor public class DB: ObservableObject { }

The DB object is shared as an environment object and is attached to the root viewin App.swift:

App.swift
// ...
@main
struct MainApp: App {
    // ...
	@StateObject var db = DB()
    // ...
    var body: some Scene {
        // ...
        ContentView()
            .environmentObject(db)
        // ...
    }
    // ...
}

To access the DB object in your views, you can use the @EnvironmentObject property wrapper:

YourView.swift
import SwiftUI
 
struct YourView: View {
    @EnvironmentObject var db: DB
 
    var body: some View {
        VStack {
 
        }
        .onAppear {
            // ... access db object after loading the view
            db.someFunction()
        }
    }
}

Refer to the Database usage example for more information on how to interact with the database.

AnalyticsKit Integration

If AnalyticsKit is selected during project generation, most of the events and errors related to database operations are automatically tracked with db as the source.

Module File Contents

Let's go through every file that is contained in the FirebaseKit module that is related to DBKit (also includes AuthKit or BackendKit related items):

Config

Contains the GoogleService-Info.plist file that you downloaded from the Firebase Console during project setup.

Sources

Model / FirebaseBackend.swift

Contains the DB observable class, which is the entry point to interact with DatabaseKit and AuthKit.

View / DatabaseExampleView.swift

The View that utilized multiple database related operations to demonstrate how to interact with the database, as well as access AuthKit user information. Read more.