Reputation: 896
I have a firebase app in swift. Now my issue is very simple to explain but really weird to debug.
Issue: Whenever I open the app, it works completely fine. Does everything it's supposed to etc. But when i just leave the app in background for a while and open it, I get a crash. I have to reopen it like 2 times for the crash to go or kill it.
Re-producing the error: step 1: Open App (do something in there or just leave it) and then just go back to home and not kill it, keeping it in background
Step 2: Open the app after a while. --- > gives crash
Step 3: Open it again after step 2 after sometime or immediately --- > crashes again
Step 4: Open it the third time. ---> works totally fine.
Note: To escape the crashes I will always have to kill it. This overall is terrible UX.
Also, I have check with my plists. they are properly functioning. Yet when it crashes I get this error :
2018-12-20 19:05:44.272413-0800 SpotMi[15557:862318] *** Terminating app due to uncaught exception 'FIRAppNotConfigured', reason: 'Failed to get default Firebase Database instance. Must call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift) before using Firebase Database.'
*** First throw call stack:
(0x1a1e2bea0 0x1a0ffda40 0x1a1d32674 0x1030c8d20 0x102f9cd24 0x102f992b0 0x102f9926c 0x10f928dc8 0x10f92ae28 0x10ef95e18 0x102f9935c 0x102f49644 0x102f4a55c 0x102f4ad94 0x102e03b18 0x102d5cd4c 0x102f49260 0x102f493b0 0x102f4ac74 0x102f4b11c 0x102dbaf3c 0x102dbafd8 0x1cea6620c 0x1cea6663c 0x1cf0551e4 0x1cf051200 0x1cf0240a0 0x1cf02585c 0x1cf02b2a8 0x1ce8c7358 0x1ce8cffd8 0x1ce8c6fd4 0x1ce8c7974 0x1ce8c5a74 0x1ce8c5720 0x1ce8ca8e0 0x1ce8cb840 0x1ce8ca798 0x1ce8cf684 0x1cf0297a0 0x1cec12bac 0x1a48609d4 0x1a486b79c 0x1a486ae94 0x10f928dc8 0x10f92ca10 0x1a489fa9c 0x1a489f728 0x1a489fd44 0x1a1dbc1cc 0x1a1dbc14c 0x1a1dbba30 0x1a1db68fc 0x1a1db61cc 0x1a402d584 0x1cf02d054 0x102d5da9c 0x1a1876bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
Appdelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
#if DEVELOPMENT
print("Development Mode Started")
let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!)
else {
fatalError("Couldn't load config file")
}
FirebaseApp.configure(options: fileopts)
#else
print("Production Mode Started")
FirebaseApp.configure()
#endif
return true
}
I tried putting this patch in the init()
, but then the notifications stops working.
How should I be tackling this issue. And what am i doing wrong?
Let me know if more info is required.
My firebase singleton is below:
class FBDataservice : NSObject {
static var ds = FBDataservice() //<-------- Always crahses on this line
let DB_URL: DatabaseReference = Database.database().reference()
let ST_URL: StorageReference = Storage.storage().reference()
private lazy var _REF_BASE = DB_URL
private lazy var _REF_POSTS = DB_URL.child("key1")
private lazy var _REF_USERS = DB_URL.child("key2")
private lazy var _ST_REF_PROFPOST = ST_URL.child("key3")
var REF_BASE: DatabaseReference! {
return _REF_BASE
}
var REF_POSTS: DatabaseReference! {
return _REF_POSTS
}
var REF_USERS: DatabaseReference! {
return _REF_USERS
}
var REF_POST_USERIMG: StorageReference {
return _ST_REF_USERPOST
}
}
Upvotes: 1
Views: 960
Reputation: 1042
I am assuming it takes too long to connect to Firebase, so when a dependency in your code is meet, application does not know what to do. You can try to put connection-related codes (Firebase.configure) to a try-catch block to see what is going on.
Upvotes: 0
Reputation: 777
AppDelegate will initialize all initial view controllers (and all primary view controllers in a tab navigation controller) and call viewDidLoad()
on them during app startup. This is by design (viewController's viewdidload called before appDelegate's method)
The problem is accessing your ds
variable in either init() or viewDidLoad()
methods. This causes a race condition to occur between the call to FirebaseApp.configure()
and each of the viewDidLoad()
methods. If ever the FirebaseApp.configure()
completes before the the viewcontroller initialization you will be fine.
To resolve this you need to take over storyboard initialization to yourself. This requires two steps:
In your AppDelegate applicationDidFinishLaunching
method initialize the required storyboard as follows:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// configure Firebase
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
return true
}
}
Upvotes: 6