mercredi 1 février 2017

Second If statement gets called before first statement finished in one function

I have one function with 2 if statements inside it. Code:

func pictureFromFirebase(loginMethod: Int)
    {
    if loginMethod == 0 //FB
    {
        var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height":300, "width":300, "redirect":false], httpMethod: "GET")
        let profilePicRef = storageRef.child((user?.uid)!+"/profile_pic.jpg")
        profilePicRef.data(withMaxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                // Uh-oh, an error occurred!
            } else {
               if (data != nil)
               {
                print("no need to download image from facebook")
                self.profileImage.image = UIImage (data: data!)
                }
            }
        }
        if profileImage.image == nil
        {
        print("downloading image from facebook")
        profilePic?.start(completionHandler: {(_ connection, _ result, _ error) -> Void in
            if (error == nil)
            {
                if let dictionary = result as? [String:Any], let data = dictionary["data"] as? [String:Any],
                    let urlPic = data["url"] as? String {
                    if let imageData = NSData(contentsOf: NSURL(string: urlPic)! as URL)
                    {
                        let uploadTask = profilePicRef.put(imageData as Data, metadata: nil){
                         metadata, error in
                            if (error == nil)
                            {
                                let downloadUrl = metadata!.downloadURL
                            }
                            else
                            {
                                print("error in downloading image")
                            }
                        }
                        self.profileImage.image = UIImage(data: imageData as Data)
                    }
                }

            }
        })
    }
    }
    }

It gets only called when this function is triggered:

FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
            print("called")
            if let user = user {
                if(activeUser != user){
                let name = user.displayName
                self.profileName.text = name
                self.pictureFromFirebase(loginMethod: 0)
                }
            }

This is the normal output from the debug, when I already have set an image so downloading an image from Facebook is not needed:

-called -downloading image from facebook -called -downloading image from facebook -no need to download image from facebook -no need to download image from Facebook

This is the output from the debug when I remove the whole second if statement (if profileImage.image == nil):

-called -called -no need to download image from facebook -no need to download image from Facebook

From what I can see, apart that the function are called twice which is also wrong, the second statement gets called before the first statement is executed. Since the first statement takes a while before the profileImage is set, its nil when the second if statement is checking this value. How can I make sure this won't happen anymore? Thank you.

Aucun commentaire:

Enregistrer un commentaire