I converted one of my apps to the new Firestore. I am doing things like saving a document on a button click, and then in the onSuccess
listener, going to a different activity.
I also use the fact that Firestore save operations return tasks, to group tasks together using Tasks.whenAll
:
val allTasks = Tasks.whenAll(
createSupporter(supporter),,
setStreetLookup(makeStreetKey(supporter.street_name)),
updateCircleChartForUser(statusChange, createMode = true),
updateStatusCountForUser(statusChange))
allTasks.addOnSuccessListener(this@SignUpActivity, successListener)
allTasks.addOnFailureListener(this@SignUpActivity, onFailureListener)
Finally, I get the document id from a successful save and store it in preferences or in a local database for later use (within the onSuccessListener
)
This all works great. Until there is a loss of network connectivity. Then everything falls apart, because the tasks never complete and the onSuccess/onFailure/onComplete listeners never get called. So the app just hangs.
I am working around this by checking for network availability before each save, and then doing a work-around by creating tasks without any listeners. I am also generating a document id locally using a UUID generator.
This, BTW, was not the way the app worked with the old firebase. In that case, everything ran nicely when offline and I saw documents getting synced up whenever the app came online.
My workaround for Firestore seems a terrible hack. Has anyone come up with a better solution?
See related Firestore database on insert/delete document callbacks not being invoked when there is no connection
addOnCompleteListener not called offline with cloud firestore
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…