Closed ksigWyatt closed 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.
I have the same bug but I dont have any Auth.auth().currentUser variable on top sir
@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?
@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.
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.
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.
Even my app is crashing!! How can I access the Auth variable globally? Any help will be appreciated.
@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:
init
/ viewDidLoad
methodnil
so you should have something like this var currentUser: User? = nil
self.currentUser = Auth.auth().currentUser
.
self.currentUser
.
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.
@ksigWyatt Thanks a lot!! It helped me solve the problem.
@nikhilagr No problem. I'm glad I could help you
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 !!
Crash Report
The default Firebase app has not yet been configured.