How to use Swift Google Promises library correctly, one of my closures never gets called?












0















I am using the Google promises library for Swift, but it is never executing a passed closure. I suspect that it might have to do with the nested promises, but can't seem to get the code to work.



internal func flow(){
loginToFacebook().then {token in
return self.signIntoFirebase(accessToken: token)
}.then { token in
return self.getUserDocumentFromFirebase(userId: token.userId!)
}.then { token, document in
if document?.exists == false {
// ### Notice no return here, is this the issue?
self.makeFacebookGraphRequest(accessToken: token).then { user in
return self.downloadFacebookProfilePictureAsImage(userModel: user)
}.then { (arg) in
let (user, image) = arg
// Never completes this function, and the closure that I pass never gets called but the image gets saved
return self.saveUserProfilePictureToFirestore(user: user, profileImage: image)
}.then { user in
return self.getProfilePictureUrlFromFirestoreForUser(user: user)
}.then { user, imageUrl in
return self.createNewUserDocumentAsDictionary(user: user, avatarUrl: imageUrl)
}.then { (arg) in
let (user, userDictionary) = arg
return self.saveUserDocumentToFirestore(user: user, data: userDictionary)
}.then {
self.performSegue(withIdentifier: "TermsViewController", sender: self)
}
} else {
self.dismiss(animated: true, completion: nil)
}
}.catch { error in
self.log.error(error.localizedDescription)
}.always {
//Dependency.dismissHud(self.hud, "Test title", "Test message text", 3)
}
}


The self.saveUserProfilePictureToFirestore(...) function gets call, the image saves to firebase, but the closure never gets called and the program seems to hang.



fileprivate func saveUserProfilePictureToFirestore(user: User, profileImage: UIImage) -> Promise<User> {
self.log.info("->")
return Promise {fulfill, reject in
guard let filename = user.uid else {
reject(ClassError.invalidUserObject)
return
}
guard let profileImageUploadData = UIImageJPEGRepresentation(profileImage, 0.3) else {
self.log.error("->Guard Failure: Image not instantiated")
reject(ClassError.couldNotGetImageData)
return
}
let ref = Storage.storage().reference().child("profileImages").child(filename+".jpg")
ref.putData(profileImageUploadData, metadata: nil) { (nil, err) in
self.log.info("--->{PutDataClosure}")
if let error = err {
self.log.error("->(error)")
reject(ClassError.couldNotsaveProfilePictureToFirestore)
}else{
fulfill(user)
}
self.log.info("<---{PutDataClosure}")
}
self.log.info("<-")

}
}









share|improve this question



























    0















    I am using the Google promises library for Swift, but it is never executing a passed closure. I suspect that it might have to do with the nested promises, but can't seem to get the code to work.



    internal func flow(){
    loginToFacebook().then {token in
    return self.signIntoFirebase(accessToken: token)
    }.then { token in
    return self.getUserDocumentFromFirebase(userId: token.userId!)
    }.then { token, document in
    if document?.exists == false {
    // ### Notice no return here, is this the issue?
    self.makeFacebookGraphRequest(accessToken: token).then { user in
    return self.downloadFacebookProfilePictureAsImage(userModel: user)
    }.then { (arg) in
    let (user, image) = arg
    // Never completes this function, and the closure that I pass never gets called but the image gets saved
    return self.saveUserProfilePictureToFirestore(user: user, profileImage: image)
    }.then { user in
    return self.getProfilePictureUrlFromFirestoreForUser(user: user)
    }.then { user, imageUrl in
    return self.createNewUserDocumentAsDictionary(user: user, avatarUrl: imageUrl)
    }.then { (arg) in
    let (user, userDictionary) = arg
    return self.saveUserDocumentToFirestore(user: user, data: userDictionary)
    }.then {
    self.performSegue(withIdentifier: "TermsViewController", sender: self)
    }
    } else {
    self.dismiss(animated: true, completion: nil)
    }
    }.catch { error in
    self.log.error(error.localizedDescription)
    }.always {
    //Dependency.dismissHud(self.hud, "Test title", "Test message text", 3)
    }
    }


    The self.saveUserProfilePictureToFirestore(...) function gets call, the image saves to firebase, but the closure never gets called and the program seems to hang.



    fileprivate func saveUserProfilePictureToFirestore(user: User, profileImage: UIImage) -> Promise<User> {
    self.log.info("->")
    return Promise {fulfill, reject in
    guard let filename = user.uid else {
    reject(ClassError.invalidUserObject)
    return
    }
    guard let profileImageUploadData = UIImageJPEGRepresentation(profileImage, 0.3) else {
    self.log.error("->Guard Failure: Image not instantiated")
    reject(ClassError.couldNotGetImageData)
    return
    }
    let ref = Storage.storage().reference().child("profileImages").child(filename+".jpg")
    ref.putData(profileImageUploadData, metadata: nil) { (nil, err) in
    self.log.info("--->{PutDataClosure}")
    if let error = err {
    self.log.error("->(error)")
    reject(ClassError.couldNotsaveProfilePictureToFirestore)
    }else{
    fulfill(user)
    }
    self.log.info("<---{PutDataClosure}")
    }
    self.log.info("<-")

    }
    }









    share|improve this question

























      0












      0








      0








      I am using the Google promises library for Swift, but it is never executing a passed closure. I suspect that it might have to do with the nested promises, but can't seem to get the code to work.



      internal func flow(){
      loginToFacebook().then {token in
      return self.signIntoFirebase(accessToken: token)
      }.then { token in
      return self.getUserDocumentFromFirebase(userId: token.userId!)
      }.then { token, document in
      if document?.exists == false {
      // ### Notice no return here, is this the issue?
      self.makeFacebookGraphRequest(accessToken: token).then { user in
      return self.downloadFacebookProfilePictureAsImage(userModel: user)
      }.then { (arg) in
      let (user, image) = arg
      // Never completes this function, and the closure that I pass never gets called but the image gets saved
      return self.saveUserProfilePictureToFirestore(user: user, profileImage: image)
      }.then { user in
      return self.getProfilePictureUrlFromFirestoreForUser(user: user)
      }.then { user, imageUrl in
      return self.createNewUserDocumentAsDictionary(user: user, avatarUrl: imageUrl)
      }.then { (arg) in
      let (user, userDictionary) = arg
      return self.saveUserDocumentToFirestore(user: user, data: userDictionary)
      }.then {
      self.performSegue(withIdentifier: "TermsViewController", sender: self)
      }
      } else {
      self.dismiss(animated: true, completion: nil)
      }
      }.catch { error in
      self.log.error(error.localizedDescription)
      }.always {
      //Dependency.dismissHud(self.hud, "Test title", "Test message text", 3)
      }
      }


      The self.saveUserProfilePictureToFirestore(...) function gets call, the image saves to firebase, but the closure never gets called and the program seems to hang.



      fileprivate func saveUserProfilePictureToFirestore(user: User, profileImage: UIImage) -> Promise<User> {
      self.log.info("->")
      return Promise {fulfill, reject in
      guard let filename = user.uid else {
      reject(ClassError.invalidUserObject)
      return
      }
      guard let profileImageUploadData = UIImageJPEGRepresentation(profileImage, 0.3) else {
      self.log.error("->Guard Failure: Image not instantiated")
      reject(ClassError.couldNotGetImageData)
      return
      }
      let ref = Storage.storage().reference().child("profileImages").child(filename+".jpg")
      ref.putData(profileImageUploadData, metadata: nil) { (nil, err) in
      self.log.info("--->{PutDataClosure}")
      if let error = err {
      self.log.error("->(error)")
      reject(ClassError.couldNotsaveProfilePictureToFirestore)
      }else{
      fulfill(user)
      }
      self.log.info("<---{PutDataClosure}")
      }
      self.log.info("<-")

      }
      }









      share|improve this question














      I am using the Google promises library for Swift, but it is never executing a passed closure. I suspect that it might have to do with the nested promises, but can't seem to get the code to work.



      internal func flow(){
      loginToFacebook().then {token in
      return self.signIntoFirebase(accessToken: token)
      }.then { token in
      return self.getUserDocumentFromFirebase(userId: token.userId!)
      }.then { token, document in
      if document?.exists == false {
      // ### Notice no return here, is this the issue?
      self.makeFacebookGraphRequest(accessToken: token).then { user in
      return self.downloadFacebookProfilePictureAsImage(userModel: user)
      }.then { (arg) in
      let (user, image) = arg
      // Never completes this function, and the closure that I pass never gets called but the image gets saved
      return self.saveUserProfilePictureToFirestore(user: user, profileImage: image)
      }.then { user in
      return self.getProfilePictureUrlFromFirestoreForUser(user: user)
      }.then { user, imageUrl in
      return self.createNewUserDocumentAsDictionary(user: user, avatarUrl: imageUrl)
      }.then { (arg) in
      let (user, userDictionary) = arg
      return self.saveUserDocumentToFirestore(user: user, data: userDictionary)
      }.then {
      self.performSegue(withIdentifier: "TermsViewController", sender: self)
      }
      } else {
      self.dismiss(animated: true, completion: nil)
      }
      }.catch { error in
      self.log.error(error.localizedDescription)
      }.always {
      //Dependency.dismissHud(self.hud, "Test title", "Test message text", 3)
      }
      }


      The self.saveUserProfilePictureToFirestore(...) function gets call, the image saves to firebase, but the closure never gets called and the program seems to hang.



      fileprivate func saveUserProfilePictureToFirestore(user: User, profileImage: UIImage) -> Promise<User> {
      self.log.info("->")
      return Promise {fulfill, reject in
      guard let filename = user.uid else {
      reject(ClassError.invalidUserObject)
      return
      }
      guard let profileImageUploadData = UIImageJPEGRepresentation(profileImage, 0.3) else {
      self.log.error("->Guard Failure: Image not instantiated")
      reject(ClassError.couldNotGetImageData)
      return
      }
      let ref = Storage.storage().reference().child("profileImages").child(filename+".jpg")
      ref.putData(profileImageUploadData, metadata: nil) { (nil, err) in
      self.log.info("--->{PutDataClosure}")
      if let error = err {
      self.log.error("->(error)")
      reject(ClassError.couldNotsaveProfilePictureToFirestore)
      }else{
      fulfill(user)
      }
      self.log.info("<---{PutDataClosure}")
      }
      self.log.info("<-")

      }
      }






      swift promise






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 9:41









      Nate UniNate Uni

      499621




      499621
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478315%2fhow-to-use-swift-google-promises-library-correctly-one-of-my-closures-never-get%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478315%2fhow-to-use-swift-google-promises-library-correctly-one-of-my-closures-never-get%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)