Last active
May 25, 2021 07:11
-
-
Save shawnkoh/ba66b8c969594b35e0ee40cc277672e5 to your computer and use it in GitHub Desktop.
How to stop SwiftUI Previews from crashing when using Firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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