Promisekit: How to use promises with firebase?

Created on 22 Jan 2017  Â·  3Comments  Â·  Source: mxcl/PromiseKit

So I import import PromiseKit and then try

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

obviously this doesnt work, and i was wondering what I'am missing to make promises work with firebase

Most helpful comment

So I import import PromiseKit and then try
FIRDatabase.database().reference().child("somechild").removeValue().then {…}
obviously this doesnt work...

We don't currently have an extension for Firebase the way we do for e.g. Alamofire, so we have no out-of-the-box functionality like this unfortunately. We probably should, as we used to for Parse and Firebase is the common replacement. We make this kind of stuff easy though. For example, to create an overload of Firebase's removeValue() that returns a Promise:

extension FIRDatabaseReference {
    func removeValue() -> Promise<FIRDatabaseReference> {
        return PromiseKit.wrap { resolve in
            removeValue(completionBlock: resolve)
        }
    }
}

Then you can use removeValue() as in your example above. This will also, in turn, let you utilize our when, as @zlangley mentioned, which acts as Promise.all from your example:

```swift
PromiseKit.when(fulfilled: [someRef1.removeValue(), someRef2.removeValue(), someRef3.removeValue(), someRef4.removeValue()]).then { values in
//…
}
````

Take note of the two variants of when though (when(resolved:) and when(fulfilled:)) as they behave differently.

All 3 comments

What I'am 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
})

what is the equivalent for that in swift

Check out when(fulfilled:).

So I import import PromiseKit and then try
FIRDatabase.database().reference().child("somechild").removeValue().then {…}
obviously this doesnt work...

We don't currently have an extension for Firebase the way we do for e.g. Alamofire, so we have no out-of-the-box functionality like this unfortunately. We probably should, as we used to for Parse and Firebase is the common replacement. We make this kind of stuff easy though. For example, to create an overload of Firebase's removeValue() that returns a Promise:

extension FIRDatabaseReference {
    func removeValue() -> Promise<FIRDatabaseReference> {
        return PromiseKit.wrap { resolve in
            removeValue(completionBlock: resolve)
        }
    }
}

Then you can use removeValue() as in your example above. This will also, in turn, let you utilize our when, as @zlangley mentioned, which acts as Promise.all from your example:

```swift
PromiseKit.when(fulfilled: [someRef1.removeValue(), someRef2.removeValue(), someRef3.removeValue(), someRef4.removeValue()]).then { values in
//…
}
````

Take note of the two variants of when though (when(resolved:) and when(fulfilled:)) as they behave differently.

Was this page helpful?
0 / 5 - 0 ratings