Giter Club home page Giter Club logo

Comments (11)

ksigWyatt avatar ksigWyatt commented on September 25, 2024 5

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.

from thrivechurchofficialapp.

ksigWyatt avatar ksigWyatt commented on September 25, 2024 1

@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.

from thrivechurchofficialapp.

nikhilagr avatar nikhilagr commented on September 25, 2024 1

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

from thrivechurchofficialapp.

DimNovo avatar DimNovo commented on September 25, 2024 1

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 !!

from thrivechurchofficialapp.

ndkhoa96 avatar ndkhoa96 commented on September 25, 2024

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

from thrivechurchofficialapp.

suttonau avatar suttonau commented on September 25, 2024

@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?

from thrivechurchofficialapp.

ryanrelf avatar ryanrelf commented on September 25, 2024

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.

from thrivechurchofficialapp.

criskgl avatar criskgl commented on September 25, 2024

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.

from thrivechurchofficialapp.

nikhilagr avatar nikhilagr commented on September 25, 2024

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

from thrivechurchofficialapp.

ksigWyatt avatar ksigWyatt commented on September 25, 2024

@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.

from thrivechurchofficialapp.

ksigWyatt avatar ksigWyatt commented on September 25, 2024

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

from thrivechurchofficialapp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.