0

I'm new in swift and SwiftUI and i have a big problem, I hope someone can help me. In fact, I have a function were I use a call to firebase Db, but the function end before the response of firebase. So is there any way to do like an await in swift ? I try to find by myself but everything I try doesn't work.

I put a sample of code, it's maybe going to be clearer.

extension SessionStore {

    func checkReferralCode(){
        let docRef = db.document(user.referredBy)

        docRef.getDocument { (document, error) in
                if let document = document, document.exists {
                    let otherUser = self.changeReferralUserInformation(dataDescription: document)
                    docRef.setData(otherUser)
                    self.user.moneyBalance = 1
                    return
                } else {
                    print("No referral Code")
                    self.firestoreError = "referralCode_unvalid"
                    self.user.referredBy = ""
                    return
                }
            }
        }

    func doInscriptionInformation()  {
        if (self.user.referredBy != "") {
            self.checkReferralCode()
            if (self.firestoreError == "" ) {
/* it's always go in this way but 1 secs after the firestoreError change */
                print("START UPLOAD")
                self.determineUploadType()
           } else {
                return
        }
    }
}

The output it's gonna be :

$> START UPLOAD
$> No referral Code
marc-medley
  • 8,931
  • 5
  • 60
  • 66
pfnts
  • 141
  • 6
  • Does this answer your question? [How to make async / await in Swift?](https://stackoverflow.com/questions/48713427/how-to-make-async-await-in-swift) – Cameron Little Apr 02 '20 at 06:48
  • if you print (error), what says the error after you changed it to my suggestion? does the class, where you declared docref still exist? show us ALL your code so that we can help you. just this few lines say nothing.... – Chris Apr 02 '20 at 07:33
  • sorry @Chris, I put the whole code – pfnts Apr 02 '20 at 08:53
  • @CameronLittle, ty i'm checking this way – pfnts Apr 02 '20 at 08:54
  • this is not the whole code - this is just an extension ;) show us a compilable runnable example...pleeeeease - and by the way, you did not do, what i proposed with your variable... – Chris Apr 02 '20 at 09:14
  • I try but i have some errors with passing throught subject, and the whole code are more than 300 lines – pfnts Apr 02 '20 at 09:25

2 Answers2

2

I found an answer using semaphore, here the link of the tutorial https://medium.com/@roykronenfeld/semaphores-in-swift-e296ea80f860

Here the code


func checkReferralCode(semaphore: DispatchSemaphore){
    let docRef = self.db.document(self.user.referredBy)

    semaphore.wait()
    docRef.getDocument { (document, error) in

        if let document = document, document.exists {
            let otherUser = self.changeReferralUserInformation(dataDescription: document)
            docRef.setData(otherUser)
            self.user.moneyBalance = 1
            semaphore.signal()
        } else {
            self.firestoreError = "referralCode_unvalid"
            self.user.referredBy = ""
            semaphore.signal()
        }
    }
}

func doInscriptionInformation()  {
    let semaphore = DispatchSemaphore(value: 1)

    DispatchQueue.global(qos: .userInteractive).async {
        if (self.user.referredBy != "") {
            self.checkReferralCode(semaphore: semaphore)
                semaphore.wait()
                if (self.firestoreError == "" ) {
                    self.determineUploadType()
                }
                else {
                    print("No good referral code")
                }
                semaphore.signal()
        }
    }
}

Hope that can help someone

pfnts
  • 141
  • 6
0

make docRef a variable of the class instead of a local variable like so:

 class WhateverClassNameYouHave: InheritedClass {
    let docRef : DocRef (whatever type it is)


     func checkCode() {
         self.docRef = /* get firebase doc */
          /* Call to firebase function */
          docRef.getDocument { (document, error) in
                  /* Do my stuff*/
              } else {
                  /*change variable to error*/
                  self.firestoreError = "referralCode_unvalid"
              }
          }
          /*But the fonction end before the response of firebase so my self.firestoreError  isn't update */
        }
Chris
  • 7,579
  • 3
  • 18
  • 38