Reputation: 679
I have a Cloud Background Function that pulls some data from an external source. Then this data is supposed to be stored in a Firestore document. But it's never stored. The document is unchanged. Here's my code:
public class MyFunction implements BackgroundFunction<PubSubMessage> {
@Override
public void accept(PubSubMessage message, Context context) {
// pulling data from external source
MyClass myObject = getData();
// db is the Firestore object and was initialized earlier
CollectionReference colRef = db.collection(MY_COL);
DocumentReference docRef = colRef.document(MY_DOC);
ApiFuture<WriteResult> future = docRef.set(myObject);
ApiFutures.addCallback(future,
new ApiFutureCallback<WriteResult>() {
@Override
public void onFailure(Throwable throwable) {
logger.log(Level.SEVERE,
"Failed to Update Object to Firestore", throwable);
}
@Override
public void onSuccess(WriteResult writeResult) {
logger.info("Update Object Document ... DONE");
}
}, Runnable::run);
}
}
The logging from onFailure
and onSuccess
never appear in the logs in the Firebase dashboard. It feels like the function is terminated before the write process is finished. The logs contain no errors and the data from the external source is pulled successfully.
How can I assure that the function does not terminate before the write process is finished?
Upvotes: 0
Views: 155
Reputation: 679
get()
has to be called.
public class MyFunction implements BackgroundFunction<PubSubMessage> {
@Override
public void accept(PubSubMessage message, Context context) {
// pulling data from external source
MyClass myObject = getData();
// db is the Firestore object and was initialized earlier
CollectionReference colRef = db.collection(MY_COL);
DocumentReference docRef = colRef.document(MY_DOC);
ApiFuture<WriteResult> future = docRef.set(myObject);
future.get();
}
}
IMHO this is a misleading name for this function because it suggests that the result of the future is returned but only after the future is finished. I would have expected something like waitAndGet()
or waitForFinish()
.
Upvotes: 1