Home > Mobile >  firestore query with bool as return in swift
firestore query with bool as return in swift

Time:04-17

I need to check if the following query return a true or false I have try with escaping but I receive an error:

here below the code of the function:

func checkBookingTime(user: UserModel, completion:@escaping(Bool) -> ()) {
      
      if let userId = user.id {
          db.collection("bookings").whereField("publisherUser", isEqualTo: userId).addSnapshotListener { querySnapshot, error in
              if let querySnapshot = querySnapshot {
                  let count = querySnapshot.documents.count
                  if(count == 0) {
                      completion(true)
                  } else {
                      completion(false)
                  }
                 
                  
                  print("number of doc: \(querySnapshot.documents.count)")
                  
              }
          }
      }
      
  }

and here is when I'm good to use it:

func loadBookingCheckTime(user: UserModel) -> Bool {
    self.bookingRepository.checkBookingTime(user: user) { (isSuccess) in
        if isSuccess {
            print("si")
        } else {
            print("no")
        }
    }
}

but I receive the following error:

Cannot convert return expression of type '()' to return type 'Bool'

Can someone give me some hint?

CodePudding user response:

Why dont u use just:

bookingRepository.checkBookingTime(user: user) { (isSuccess) in
            if isSuccess {
                print("si")
            } else {
                print("no")
            }
        }

You have your bool in isSucess.

func loadBookingCheckTime(user: UserModel) -> Bool can be skipped.

CodePudding user response:

You need to use a completion handler in your loadBookingCheckTime if you want to do it that way. However, as @saro mentioned, you could just call it directly.

  • Related