0

I'm developing an Android app with Kotlin, and I want to fill the Customer data class I've created with values from Firebase.

It's impossible to return from an .addOnCompleteListener after .get, therefore, I can't just return Customer, which would be ideal. I've read about toObject, but am not sure how to correctly use it (or if it does solve my problem).

Customer.kt:

data class Customer (
    val name: String,
    val phone: String,
    val address: String,
    val lastVisit: String,
    val lastVisitTimestamp: Timestamp,
    val latitude: Double,
    val longitude: Double
) : Serializable

Pseudo-code for what I want to achieve:

ref.whereEqualTo("name", name)
            .get()
            .addOnCompleteListener { task ->
                val name = task.result?.documents!![0].data!!["name"].toString()
                val customer = Customer()
                customer.name = name
                // I want to return this customer object. That's what I can't do.
                return Customer()
            }
// and so on
MucaP
  • 992
  • 1
  • 10
  • 18
  • I don't have one, but I'll put a generic one for example @a_local_nobody – MucaP Aug 24 '19 at 20:47
  • Maybe edit the question to focus on the single problem at hand? Please show code that you've tried and what didn't work the way you expect. – Doug Stevenson Aug 24 '19 at 20:49
  • if you're looking for ways to get data from the async operation, this will work : https://stackoverflow.com/questions/57330766/how-to-get-data-from-any-asynchronous-operation-in-android/57330767#57330767 – a_local_nobody Aug 24 '19 at 20:49
  • and to convert an object from firebase, all you have to do is something like : `val response = result.toObject(Customer::class.java)` – a_local_nobody Aug 24 '19 at 20:50
  • @DougStevenson I've removed references to what I might do later with this class to avoid confusion, since that was supposed to be nothing but context. I haven't tried anything to be honest, I have no idea how to do this. I can use the data from the query inside the onComplete listener, but I don't know how to get it out of there. – MucaP Aug 24 '19 at 20:51
  • look at this for help doing that : https://stackoverflow.com/questions/57330766/how-to-get-data-from-any-asynchronous-operation-in-android/57330767#57330767 – a_local_nobody Aug 24 '19 at 20:52
  • I think you might just want to start with the documentation to see how to get fields out of a DocumentSnapshot to pass to the constructor of your data class. There is also plenty of API docs that walk you through each object and the methods they offer. https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document – Doug Stevenson Aug 24 '19 at 20:54
  • @DougStevenson I know how to manipulate data INSIDE the onCompleteListener. I don't know how to get it out of there without a callback. I've added an example of what I wanted to achieve. – MucaP Aug 24 '19 at 21:11
  • why not just use a callback ? – a_local_nobody Aug 24 '19 at 21:18
  • @a_local_nobody Not to be rude or anything, but that doesn't really come to the case. Either ways. I'd like to start an activity and pass that object returned from the firebase query as an Extra, and use the values there. – MucaP Aug 24 '19 at 21:20
  • Firebase loads data from its servers asynchronously. That means that your `return Customer()` will not work, because there's nobody listening. Any code that needs access to data from the database must be inside the completion listener, or be called from there. See for an example https://stackoverflow.com/a/51002413 – Frank van Puffelen Aug 25 '19 at 04:08

0 Answers0