CombineFirebase icon indicating copy to clipboard operation
CombineFirebase copied to clipboard

FirebaseDatabase Snapshot to custom object

Open CureleaAndrei opened this issue 4 years ago • 4 comments

Hi, awesome job on this library, I will use it in my projects. It will be awesome to have the mapper functionality for firebase database snapshot, also build in.

CureleaAndrei avatar Jun 14 '20 09:06 CureleaAndrei

Actually that is already build in, you have to dig little more in docs.

Few bits:

var cityDocumentSnapshotMapper: (DocumentSnapshot) throws -> City? {
    {
        var city =  try $0.data(as: City.self)
        city.id = $0.documentID
        return city
    }
}

func listenDocumentAsObject() {
    db.collection("cities")
        .document("SF")
        .publisher(as: City.self, documentSnapshotMapper: cityDocumentSnapshotMapper)
        .sink(receiveCompletion: { completion in
            switch completion {
            case .finished: print("🏁 finished")
            case .failure(let error): print("❗️ failure: \(error)")
            }
        }) { city in
            print("City: \(city)")
        }
        .store(in: &cancelBag)
}

kshivang avatar Jun 14 '20 16:06 kshivang

Hi, thanks for the quick reply. I've found this, but this is for firestore database, i meant that would be nice to have this also for the firebase realtime database. If there is also for real time database, my bad. I will look again into docs.

CureleaAndrei avatar Jun 14 '20 18:06 CureleaAndrei

Thank you for pointing out, that functionality is missing. For now you can simply use combine .map, will surely add that feature in the future. PR is also welcome.

kshivang avatar Jun 14 '20 18:06 kshivang

@CureleaAndrei you could do this with https://github.com/alickbass/CodableFirebase

ericlewis avatar Oct 06 '20 14:10 ericlewis