gpt4 book ai didi

android - Firestore 离线写入/添加不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:45 25 4
gpt4 key购买 nike

我们能够让 Firestore 将记录下载到本地缓存并读取这些记录。但是,我们无法更新记录,也无法添加新记录。

甚至尝试了一些相同的代码 ( https://github.com/firebase/quickstart-android ),但似乎无法使离线写入/插入工作 - 只能读取。

当我们离线调用此函数时,它会调用 OnFailureListner 并抛出异常

 @Override
public void onRating(Rating rating) {
// In a transaction, add the new rating and update the aggregate totals
addRating(mRestaurantRef, rating)
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Rating added");

// Hide keyboard and scroll to top
hideKeyboard();
mRatingsRecycler.smoothScrollToPosition(0);
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Add rating failed", e);

// Show failure message and hide keyboard
hideKeyboard();
Snackbar.make(findViewById(android.R.id.content), "Failed to add rating",
Snackbar.LENGTH_SHORT).show();
}
});
}

最佳答案

您不能简单地离线写入数据,因为 Firestore 可以在您有互联网连接时获取数据并上传(这需要在后台执行任务)。 Firestore 改进了其离线功能,但您始终可以选择创建自己的服务,以便在设备连接时上传它。

Android 允许您创建服务。来自 developer.android.com :

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it. A service is implemented as a subclass of Service.

因此,您可以创建一个 Service,它一直在等待 Internet 连接,并且只调用 FirebaseFirestore 对象来上传您想要的所有数据。

这可能对您有所帮助,这是有关服务的官方 Android 信息:https://developer.android.com/guide/components/services.html

关于android - Firestore 离线写入/添加不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47089225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com