热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Firestore脱机写入/添加不起作用-FirestoreOfflineWrite/AddNotWorking

WeareabletogetFirestoretodownloadrecordstothelocalcacheandcanreadthoserecords.Howe

We are able to get Firestore to download records to the local cache and can read those records. However, we are unable to update a record and not able to add a new record.

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

Have even tried some of the same code (https://github.com/firebase/quickstart-android) but don't seem to be able to get the offline write/insert working - only read.

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

When we call this function offline it gets called OnFailureListner and throws an exception

当我们将此函数调用为脱机时,它将被调用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() {
                @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();
                }
            });
}

1 个解决方案

#1


-2  

You can't simply write data offline because Firestore can get the data and upload it whenb you have an Internet connection (which requires a task going in the background). Firestore has improved its offline capabilities but you can always choose to create your own service to upload it when the device connects.

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

Android lets you create Services. From developer.android.com:

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.

服务是在后台运行以执行长时间运行操作或执行远程进程工作的组件。服务不提供用户界面。例如,当用户在不同的应用程序中时,服务可能在后台播放音乐,或者它可能通过网络获取数据而不阻止用户与活动的交互。另一个组件(如活动)可以启动服务并让它运行或绑定到它以与之交互。服务是作为服务的子类实现的。

So you could create a Service which keeps waiting for an Internet connection and just calls the FirebaseFirestore object to upload all the data you want.

因此,您可以创建一个等待Internet连接的服务,只需调用FirebaseFirestore对象即可上传所需的所有数据。

This may help you, this is the official Android information about services: https://developer.android.com/guide/components/services.html

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


推荐阅读
author-avatar
婉里去_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有