Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PromiseKit with Firebase in Swift?

So I import PromiseKit and then try

 FIRDatabase.database().reference().child("somechild").removeValue().then  {
///// 
}

Obviously, this doesn't work and I was wondering what am I missing to make promises work with Firebase, if its even possible. What I'm trying to accomplish is to remove four Firebase references all at once with a single catch method.

With nodeJs I would easily use:

 Promise.all ([
someRef.remove(),
 someRef.remove(),
someRef.remove(),
someRef.remove()
]).then (function({

}).catch({
//handle error
})

Is there a way to accomplish this in Swift at all?

like image 292
Ryan Avatar asked Oct 18 '22 18:10

Ryan


1 Answers

you can wrap Firebase function with fulfill and reject

/// Get chat IDs of user conversations
///
/// - Returns: array of user chat IDs
private func getUserChatIds() -> Promise<[String]> {

    return Promise { fulfill, reject in
        let userChatIDsRef = Database.database().reference()
            .child(FireDatabasePaths.UserInfoPath.rawValue)
            .child(userID).child("chatIDs")

        userChatIDsRef.observe(.childAdded, with: { snapshot in

            if let chatIDdic = snapshot.value as? [String: AnyObject] {
                let keys = Array(chatIDdic.keys)
                fulfill(keys)
            } else {
                reject(FirebaseError.empty)
            }

        })
    }
}
like image 192
Hashem Aboonajmi Avatar answered Oct 21 '22 05:10

Hashem Aboonajmi