I want to create a function to check if user_id is already in my database.
class func checkIfUserExsits(uid:String) -> Bool { userRef.childByAppendingPath(uid).observeSingleEventOfType(.Value, withBlock: { (snapShot: FDataSnapshot!) -> Void in if snapShot.value is NSNull { return false } else { return true } }) }
However, observeSingleEventOfType is an API provided by third-party Firebase. It is defined as return Void .
- (void) observSingleEventOfType: (FEventType) eventType withBlock: (void (^) (FDataSnapshot * snapshot)) block
Error: Type 'Void' does not conform to protocol 'BooleanLiteralConvertible'
Appreciate any kind of help.
UPDATE
I try differently:
class func checkIfExist(uid: String) -> Bool { var answer:Bool = false var text:String = "not yet completed" let queue = dispatch_group_create() dispatch_group_enter(queue) userRef.childByAppendingPath(uid).observeSingleEventOfType(.Value, withBlock: { (snapShot: FDataSnapshot!) -> Void in if snapShot.value is NSNull { text = "This is a new user" answer = false dispatch_group_leave(queue) } else { text = "Found the user in Firebase" answer = true dispatch_group_leave(queue) } }) dispatch_group_wait(queue, DISPATCH_TIME_FOREVER) println(text) return answer }
Somehow he just freezes there. I know that this approach may be off topic. But please help.
closures return swift objective-c-blocks firebase
Ivan Wang
source share