SwiftUI 2.0 brought us a lot of new features. My favorite is the new application lifecycle because it is simpler than the old one, but configuring it with Firebase can be tricky.

In this article, we will learn how to configure Firebase with SwiftUI’s new lifecycle.

First, let’s look at the difference between the new and old app lifecycle!

Note: You can download this article’s example project and sourceson GitHub.

UIKit App Delegate Lifecycle vs. SwiftUI Lifecycle

With UIKit’s lifecycle, you need to write Firebase’s configure() function in the didFinishLaunchingWithOptions method. This method is for the operations that should be done when the application is opened for the first time.

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

 	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
		FirebaseApp.configure()
	    return true
	}
}

SwiftUI’s lifecycle is different from UIKit’s because SwiftUI is a declarative framework. That’s why there are different methods and classes. Obviously, the methods we will use in the SwiftUI lifecycle will be slightly different.

If you have used SwiftUI and UIKit together before, you know that some classes or methods we want to use in SwiftUI are from UIKit, as many things are still not supported in SwiftUI. We will get help from UIKit while adapting the SwiftUI app lifecycle to Firebase.

At the moment, I cannot say that there is a significant difference between them. Maybe things will change with SwiftUI 3.0.

#firebase

How To Use Firebase in SwiftUI’s New Application Lifecycle
1.60 GEEK