Unexpected non-void return value in void function - Swift 4 (Firebase, Firestore)

Niall Kiddle

So I want a Boolean function that returns true or false depending on whether the given email exists for a user in the users collection.

However if I try and return True or False within the getDocuments call I get the error: non-void return value in void function

func checkUserWith(email: String) -> Bool
{
    let usersDB = database.collection("users")
    usersDB.whereField("email", isEqualTo: email).getDocuments { (snapshot, error) in

        if error != nil
        {
            print("Error: \(error?.localizedDescription ?? "")")
            return false
        }

        for document in (snapshot?.documents)! {
            if document.data()["email"]! as! String == email {
                return true
            }
        }

        return false
    }
}

I have a feeling it is because I am trying to return a boolean within the Firestore call which is expecting a different variable type?

emrepun

Since the firebase operation gives you a callback closure, and the calls made asynchronously, I believe it wont be possible for you to directly return from closures. However, you can return an escaping closure indicating true or false as follows...

func checkUserWith(email: String, completion: @escaping (Bool) -> Void)
{
    let usersDB = database.collection("users")
    usersDB.whereField("email", isEqualTo: email).getDocuments { (snapshot, error) in

        if error != nil
        {
            print("Error: \(error?.localizedDescription ?? "")")
            completion(false)
        }

        for document in (snapshot?.documents)! {
            if document.data()["email"]! as! String == email {
                completion(true)
                return
            }
        }

        completion(false)
    }
}

Then when you call this method:

checkUserWith(email: emailHere) { (isSucceeded) in
    if isSucceeded {
        //it exists, do something
    } else {
        //user does not exist, do something else
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

Unexpected non-void return value in void function (Swift 3)

Swift NumberOfRowsInSection Unexpected non-void return value in void function

unexpected non void return value in void function

Unexpected non-void return value in void function swift 4 using json serialisation

Unexpected non-void return value in void function

'Unexpected non-void return value in void function' in swiftui

unexpected non-void return value in void function in new Swift class

Unexpected non-void return value in void function Swift3

Unexpected non-void return value in void function(swift3)

Class understanding: Unexpected non-void return value in void function (swift 3)

why in this code would I be getting "Unexpected non-void return value in void function"

Retrieve product localised price - Unexpected non-void return value in void function

Why does Unexpected non-void return value in void function happen?

swift Non-void function should return a value in a guard let

Return value void * in a function

How to return nil object in non void function - swift 2.1?

Restrict function return value to void

Swift 3: Cannot call value of non-function type '(() -> Void)?'

Is the return command in a non-void function necessary?

Type No return, in function returning non-void

Non-void function potential lack of return

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

What's the return value of a non-void function missing return statement?

Warning 'return' with no value, in function returning non-void - What should it return?

void function pointer return value in C

Unclear about return value of a void function in C

Should i return a value for a Future<void> Function?

Why does a void function return a value?