Skip to content

Instantly share code, notes, and snippets.

@shawnkoh
Last active May 25, 2021 07:11
Show Gist options
  • Save shawnkoh/ba66b8c969594b35e0ee40cc277672e5 to your computer and use it in GitHub Desktop.
Save shawnkoh/ba66b8c969594b35e0ee40cc277672e5 to your computer and use it in GitHub Desktop.
How to stop SwiftUI Previews from crashing when using Firebase
protocol FirebaseService {
func configure()
}
class RealFirebaseService: FirebaseService {
func configure() {
FirebaseApp.configure()
let settings = FirestoreSettings()
Firestore.firestore().settings = settings
}
}
class MockFirebaseService: FirebaseService {
func configure() {
// Do nothing because mocks do not use Firebase.
}
}
class AppViewModel: ObservableObject {
@Injected var firebaseService: FirebaseService
init() {
firebaseService.configure()
}
}
@main
struct ExampleApp: App {
@StateObject var viewModel = AppViewModel()
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment