You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone,
I'm using SkipFirebase and I understood we have to do the decoding ourselves, which is easy for most types.
However, I can't figure out how to decode DocumentReference objects or access their content on Android.
For normal types I fetch the [String: Any] dictionary with the .data() function which I then pass into JSONSerialization.data before decoding it as JSON. However, as the serialization fails with DocumentReference, I tried to obtain the documentID directly and pass them as String to my models.
Casting the reference is ignored on Android, I can't obtain more than the type name and have not yet found a way to see the content of the reference. I understood the type is different in Kotlin but I have not found how to manipulate it.
func sanitizeFirestoreData(_ data: [String: Any]) -> [String: Any] {
var sanitized = [String: Any]()
for (key, value) in data {
sanitized[key] = sanitizeFirestoreDataElement(value)
}
return sanitized
}
func sanitizeFirestoreDataElement(_ value: Any) -> Any {
if let documentRef = value as? DocumentReference { //Ignored on Android
return documentRef.documentID
} else { //Dates, array, maps also sanitized
return value
}
}
func fetchAndDecode() async throws {
let fetched = try await NetworkingManager.fetchDocument(withId: "123") //Returns [String: Any]
let sanitizedData = sanitizeFirestoreData(fetched)
let jsonData = try JSONSerialization.data(withJSONObject: sanitizedData, options: []) //Always fails at this point on Android
let decoded = try JSONDecoder().decode(TestType.self, from: jsonData)
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everyone,
I'm using SkipFirebase and I understood we have to do the decoding ourselves, which is easy for most types.
However, I can't figure out how to decode DocumentReference objects or access their content on Android.
For normal types I fetch the [String: Any] dictionary with the .data() function which I then pass into JSONSerialization.data before decoding it as JSON. However, as the serialization fails with DocumentReference, I tried to obtain the documentID directly and pass them as String to my models.
Casting the reference is ignored on Android, I can't obtain more than the type name and have not yet found a way to see the content of the reference. I understood the type is different in Kotlin but I have not found how to manipulate it.
Beta Was this translation helpful? Give feedback.
All reactions