简体   繁体   English

强制数据与Android的firebase服务器同步

[英]Force data sync with firebase server from Android

I'm working on an Android app which has sqlite database. 我正在开发一个拥有sqlite数据库的Android应用程序。 I wanted to add synchronisation for the db with some cloud service and across multiple devices of the same user. 我想为数据库添加一些云服务和同一用户的多个设备的同步。 There will be login mechanism and data will be different for different users. 将有登录机制,不同用户的数据会有所不同。 The synchronisation will be for logged in users only. 同步仅适用于登录用户。 For others the data will reside locally only. 对于其他人,数据仅驻留在本地。

I found firebase very interesting and started adding it for the synchronisation. 我发现firebase非常有趣,并开始添加它以进行同步。 I cannot completely replace sqlite with firebase because I need to provide offline experience for users that are not logged in. 我无法用firebase完全替换sqlite,因为我需要为未登录的用户提供离线体验。

I implemented syncadapter for the sync operation. 我为同步操作实现了syncadapter。 And I'm doing the sqlite<->firebase two way synchronisation there. 我正在那里进行sqlite < - > firebase双向同步。

I'm using firebase transactions to avoid conflict from multiple devices. 我正在使用firebase事务来避免来自多个设备的冲突。 But I read that firebase Android initially writes on offline database and sync with server at a later time. 但是我读到firebase最初写在脱机数据库上并在稍后与服务器同步。 This may cause transactions to fail sometimes creating inconsistencies in data. 这可能会导致事务失败,有时会导致数据不一致。 And also I think transactions are not maintained across app restarts. 而且我认为交易不会在应用重启时保持不变。

If there was an option to disable offline feature for firebase and each request is sent to server instantly and finish with a successful completion or a failure I may be able to implement the synchronisation correctly here. 如果有一个选项可以禁用firebase的脱机功能,并且每个请求都会立即发送到服务器并成功完成或失败,我可以在此处正确实现同步。

So is there any way to force to do write and read directly to and from server? 那么有没有办法强制直接写入和读取服务器? Is there a better way to do this ensuring good conflict handling across multiple devices? 有没有更好的方法来确保跨多个设备的良好冲突处理? Is firebase not suited for this purpose? firebase不适合这个目的吗?

If someone is still looking for the solution. 如果有人仍在寻找解决方案。

While using firebaseRef.addValueEventListener() initially it loads data from available cache, then requests updated data from server. 在使用firebaseRef.addValueEventListener()时,它最初从可用缓存加载数据,然后从服务器请求更新的数据。

In order to retrieve latest server side data, We made sure user is connected to internet and also connected to firebase server database. 为了检索最新的服务器端数据,我们确保用户连接到Internet并连接到firebase服务器数据库。

        DatabaseReference connectionRefrence;
        ValueEventListener connectionValueEventListener;

        connectionRefrence = FirebaseDatabase.getInstance().getReference(".info/connected");
        connectionValueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                boolean connected = dataSnapshot.getValue(Boolean.class);
                if (connected) {
                    printLog("connected");
                    // TODO :: Perform your operation here with live data

                } else {
                    printLog("not connected");
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                printLog("connection cancelled");

            }
        };

I hope it helps. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM