ThriveCommunityChurch / ThriveChurchOfficialApp

Official Repo for the Thrive Church Official App, only on iOS. Download now for free.
http://thrive-fl.org/
5 stars 1 forks source link

FirebaseApp.configure() causes crash but is present #57

Closed ksigWyatt closed 6 years ago

ksigWyatt commented 6 years ago

Crash Report

The default Firebase app has not yet been configured.

[Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add [FIRApp configure]; (FirebaseApp.configure() in Swift) to your application initialization.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The default FIRApp instance must be configured before the default FIRAuthinstance can be initialized. One way to ensure that is to call [FIRApp configure]; (FirebaseApp.configure() in Swift) in the App Delegate's application:didFinishLaunchingWithOptions: (application(_:didFinishLaunchingWithOptions:) in Swift).'

ksigWyatt commented 6 years ago

Removed Auth.auth().currentUser from the top variables. Not sure how that would cause a crash on another page but okay then. We're back in business.

ndkhoa96 commented 6 years ago

I have the same bug but I dont have any Auth.auth().currentUser variable on top sir

suttonau commented 6 years ago

@ksigWyatt Same for me - Everything was working fine but then I made a global variable let currentUserId = Auth.auth().currentUser?.uid and then the app started crashing. Just moved the variable inside my one of my methods and everything is fine now. Thanks for your help I didn't know what was causing the error for a while. I wonder why it does this?

ksigWyatt commented 6 years ago

@suttonau @ndk1996 Thanks for your comments. Interesting that @ndk1996 I didn't see your comment until now.

So when I think about why this might happen (I'm not 100% sure why either). I think this line let currentUserId = Auth.auth().currentUser?.uid (still happens with var too) is an invalid variable declaration because it cannot retrieve currentUser?.uid. This is because if there is no user logged in the uid will be nil. Now that wouldn't necessarily cause a crash, by assigning nil to a variable. It will if you're using currentUserId for something.

But I think ultimately the reason that this happens is because the global variable created is attempting to make this reference to currentUser before the app can use handle to check if the user is logged in, using something like line 11 and following:

// global vars
fileprivate var handle: AuthStateDidChangeListenerHandle?
var user: User? // not sure if this is required but it might be helpful to refer to the User as just user

// inside either viewDidLoad or ViewDidAppear
// Listen for Auth State changes
self.handle = Auth.auth().addStateDidChangeListener(self.handleAuthStateChanged(auth:user:))

// Check status of user
func handleAuthStateChanged(auth: Auth, user: User?) {

    if Auth.auth().currentUser != nil {
        // User is signed in.
    }
    else {
        // Login OR Register -- only if their email is not already in Auth
    }
}

Once this runs we have the ability to reference anything under Auth.Auth().currentUser. Also, the code block above can happen on another thread so there's no way of knowing exactly when this will run. So what I would recommend is include a check in the AppDelegate. Or inside your View Controller that you're using currentUser?.uid. Making this a local variable solves this issue for this reason. I'd imagine that perhaps using this (might?) work filepriveate currentUserId = Auth.auth().currentUser?.uid. But I doubt it.

Hopefully that helps you guys a little bit with understanding how this happens. Then again that's just what makes sense to me. I could be wrong.

ryanrelf commented 5 years ago

I experienced the same issue with the Auth variable. I wanted to access it globally so I created a global variable for it var user = Auth.auth().currentUser- This caused my app to crash.

criskgl commented 5 years ago

Im experiencing the same issue when trying to connect sign in button to tab bar view controller I fixed it redirecting the segway to another view.

nikhilagr commented 5 years ago

Even my app is crashing!! How can I access the Auth variable globally? Any help will be appreciated.

ksigWyatt commented 5 years ago

@nikhilagr It seems as though you cannot access this globally. You must assign it to a value whenever you want to use it.

What I'd recommend is something like this:

  1. Declare a global variable outside your init / viewDidLoad method
  2. Assign this nullable variable a default value of nil so you should have something like this var currentUser: User? = nil
  3. Within your init / viewDidLoad method, assign the current user to your global variable via something like this self.currentUser = Auth.auth().currentUser.
    • NOTE: Assuming that a user is currently logged in, this value will contain something other than nil. You could put a breakpoint here just to verify yourself that this in fact works as we expect.
  4. Anyplace within your view controller you need to use the current user. Simply use self.currentUser.
    • Now, there is no guarantee that self.currentUser will continue to hold this value. If the user is logged out our self.currentUser may return to nil. Because we are creating a reference type; See more on 'Deep Copy vs Shallow Copy'

So I would expect this to work, where you can access self.currentUser within a view controller. You (May?) be able to get away with passing it around to other View Controllers within a DidSet block, but again if the user logs out, then this value will have changed.

nikhilagr commented 5 years ago

@ksigWyatt Thanks a lot!! It helped me solve the problem.

ksigWyatt commented 5 years ago

@nikhilagr No problem. I'm glad I could help you

DimNovo commented 5 years ago

Removed Auth.auth().currentUser from the top variables. Not sure how that would cause a crash on another page but okay then. We're back in business.

!! Thank you very much dr @ksigWyatt !!