5

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?

AL.
  • 36,815
  • 10
  • 142
  • 281
Ryan
  • 969
  • 1
  • 6
  • 19
  • 1
    This question is a bit vague as it's not clear what the use case is; i.e. what is the correlation between Promise and Firebase for your case? Firebase is asynchronous already so there's a number of ways to remove child nodes in both a synchronous fashion as well as asynchronously. See my answer here as it may help [Delete Several Child Values](http://stackoverflow.com/questions/38462074/using-updatechildvalues-to-delete-from-firebase/38466959#38466959) – Jay Jan 22 '17 at 15:16
  • i did not know i could do that, thats really helpful man ! – Ryan Jan 22 '17 at 16:20
  • I was looking for something similar... What I ended up doing was a smaller "PromiseKit" using Generics... like Promise and when the snapshot is filled, it calls a delegate on the view and fills the info... – Raphael Ayres Apr 19 '17 at 03:49

1 Answers1

3

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)
            }

        })
    }
}
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75