[英]NSInternalInconsistencyException: Firebase crashes SwiftUI app on launch
I am trying to add Firebase auth via Apple Sign-in and have got this error:我正在尝试通过 Apple 登录添加 Firebase 身份验证,但出现此错误:
crashed due to an uncaught exception `NSInternalInconsistencyException`. Reason: The default FirebaseApp instance must be configured before the default Authinstance can be initialized. One way to ensure this is to call `FirebaseApp.configure()` in the App Delegate's `application(_:didFinishLaunchingWithOptions:)` (or the `@main` struct's initializer in SwiftUI)
This is how I launch Firebase:这就是我启动 Firebase 的方式:
import Firebase
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
FirebaseApp.configure()
return true
}
}
I did not install the Firebase package directly, but instead added this package. which I believe added Firebase as one of its dependency.我没有直接安装 Firebase package,而是添加了这个 package。我相信它添加了 Firebase 作为其依赖项之一。 I have checked other solutions but they all rely on CocoaPods while I'm using the Swift Package Manager.
我已经检查了其他解决方案,但当我使用 Swift Package 管理器时,它们都依赖于 CocoaPods。 Not sure how I can resolve this error.
不确定如何解决此错误。
App calling AppDelegate
:应用调用
AppDelegate
:
import FirebaseService
import SwiftUI
@main
struct SomeApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@State private var authState = AuthState()
var body: some Scene {
WindowGroup {
switch authState.value {
case .undefined:
ProgressView()
case .authenticated:
ContentView()
case .notAuthenticated:
AuthView()
}
}
}
}
This error message indicates that another part of your app tries to access Firebase Auth before Firebase is initialised in your your app delegate.此错误消息表明您的应用程序的另一部分在您的应用程序委托中初始化 Firebase 之前尝试访问 Firebase Auth。
Looking at the code of Rebeloper's package which you're using, we can see that it calls Auth.auth().addStateDidChangeListener
in line 16 of AuthListener .查看您正在使用的Rebeloper 的 package的代码,我们可以看到它在 AuthListener 的第 16 行调用
Auth.auth().addStateDidChangeListener
addStateDidChangeListener 。 Since you create an instance of AuthState
(another class from Rebeloper's library), this is executed before Firebase is initialised.由于您创建了一个
AuthState
实例(Rebeloper 库中的另一个 class),因此它在 Firebase 初始化之前执行。
Instead of using an undocumented third party library, I would recommend implementing this yourself - especially since it's only a few lines of code.与其使用未记录的第三方库,我建议您自己实现它——特别是因为它只有几行代码。
To see how this is done, check out Getting started with Firebase Auth for Apple platforms - Firebase Fundamentals , which not only explains Firebase Auth and how to handle authentication state in detail, but also includes the corresponding source code.具体是如何实现的,请查看Getting started with Firebase Auth for Apple platforms - Firebase Fundamentals ,其中不仅详细解释了 Firebase Auth 以及如何处理 state 身份验证,还包括相应的源代码。
As an added bonus, this contains a beautiful signup/login form:作为额外的奖励,它包含一个漂亮的注册/登录表单:
For reference, here is the source code for registering an authentication state listener in an ObserableObject
:作为参考,以下是在 ObserableObject 中注册身份验证
ObserableObject
侦听器的源代码:
@MainActor
class AuthenticationViewModel: ObservableObject {
@Published var email = ""
@Published var password = ""
@Published var confirmPassword = ""
@Published var flow: AuthenticationFlow = .login
@Published var isValid = false
@Published var authenticationState: AuthenticationState = .unauthenticated
@Published var errorMessage = ""
@Published var user: User?
@Published var displayName = ""
init() {
registerAuthStateHandler()
// ...
}
private var authStateHandler: AuthStateDidChangeListenerHandle?
func registerAuthStateHandler() {
if authStateHandler == nil {
authStateHandler = Auth.auth().addStateDidChangeListener { auth, user in
self.user = user
self.authenticationState = user == nil ? .unauthenticated : .authenticated
self.displayName = user?.email ?? ""
}
}
}
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.