urban-health-labs/CombineFirebase

FirebaseDatabase Snapshot to custom object

CureleaAndrei opened this issue ยท 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.

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

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.

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.