urban-health-labs / CombineFirebase

Combine wrapper on Google's iOS Firebase library.
MIT License
225 stars 44 forks source link

FirebaseDatabase Snapshot to custom object #7

Open CureleaAndrei opened 4 years ago

CureleaAndrei commented 4 years ago

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.

kshivang commented 4 years ago

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)
}
CureleaAndrei commented 4 years ago

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.

kshivang commented 4 years ago

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.

ericlewis commented 4 years ago

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