Firebase如何工作与共享数据同步? [英] How does work Firebase sync, with share data?

查看:357
本文介绍了Firebase如何工作与共享数据同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebase来处理我的Android应用程式的Auth主题。
我也保存在Firebase的用户配置文件,包含用户ID和额外的选项,用户可以在Android应用程序更新。



在启动时, auth和auth。它重新加载用户配置文件(在Firebase上),然后更新应用程序上的userInstance。



Firebase在应用程序级别设置为离线。
userInstance POJO在日志时停止与Firebase同步,并在unlog时同步。



我有一个特殊的参数,在我的用户配置文件中,我可以更改(作为管理员)。



每当我在Firebase控制台上更新时,应用程式重新开始时,都会以上一个值取代。



如何实现?



BTW:
1 /基于哪个机制是数据合并,如果多个客户端有不同的本地值? / p>

这里是更简单的代码,我试图重现错误。 :



MyApplication.java

  public class MyApplication extends应用程序{
@Override
public void onCreate(){
super.onCreate();
Firebase.setAndroidContext(this);
Firebase.getDefaultConfig()。setLogLevel(Logger.Level.DEBUG);
Firebase.getDefaultConfig()。setPersistenceEnabled(true);


}
}

MainActivity

  public class MainActivity extends AppCompatActivity {

Firebase ref;
用户;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ref = new Firebase(https://millezim-test.firebaseIO.com).child(user);
ref.keepSynced(true);

按钮br =(Button)findViewById(R.id.b_read);
按钮bs =(Button)findViewById(R.id.b_save);
final TextView tv_r =(TextView)findViewById(R.id.tv_value_toread);
final EditText tv_s =(EditText)findViewById(R.id.tv_value_tosave);

user = new User();

bs.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(!tv_s.getText()。 toString()。equalsIgnoreCase())
user.setAge(Integer.valueOf(tv_s.getText()。toString()));
ref.setValue(user);
}
});

br.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ref.addListenerForSingleValueEvent(new ValueEventListener
@Override
public void onDataChange(DataSnapshot dataSnapshot){
用户u = dataSnapshot.getValue(User.class);
if(u!= null)
tv_r .setText(String.valueOf(u.getAge()));
else
tv_r.setText(Bad Value);
}

@Override
public void onCancelled(FirebaseError firebaseError){

}
});
}
});

ref.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
用户u = dataSnapshot.getValue(User.class) ;
u.setCounter(u.getCounter()+ 1);
user = u;
saveUser();
}

@Override
public void onCancelled(FirebaseError firebaseError){

}
});
}

public void saveUser(){
ref.setValue(user);
}
}

如果只是更改应用程序中的值,计数器直到app停止:这是正常的。但是,是什么是年龄从古到旧,再循环到古老而不停止。





我觉得在我的应用程序的行为,没有循环,因为我没有一个计数器,但我不能更改在管理客户端中的参数,我总是得到存储在移动的以前的值。 / p>

我只是Auth,然后更新我的UserInstance与AuthData +用户从Firebase提取(可能是缓存的数据),然后我保存回更新的用户下Firebase我可能有新的AuthData,我通常从Firebase获得最新的用户)



2在这个简单的例子中,我看到如果我在开始读取值,获取缓存在应用程序中的数据。如何强制拥有在线数据?

解决方案

问题来自于您使用磁盘持久性,值事件侦听器。当你这样做:

  ref.addListenerForSingleValueEvent(new ValueEventListener(){... 

您要求该位置的唯一值(在您的情况下为用户)。如果Firebase在其本地缓存中具有该位置的值,它将立即触发该值。



解决这个问题的最好方法是不使用单值监听器,而是使用常规事件监听器。方式你会得到两个事件:一个用于缓存版本,一个用于从服务器返回的版本(如果不同)。



唯一的选择是不使用Firebase的磁盘持久化,否则,将不会有重新启动时读取的数据的本地缓存。



有一些讨论组合在Firebase邮件列表上。这里是一个: https://groups.google.com/ forum /#!msg / firebase-talk / ptTtEyBDKls / XbNKD_K8CQAJ


I use Firebase to handle the Auth topic of my Android app. I also save a user profile on Firebase, that contain user id and extra options that user can update in the android app.

At startup, the app check the auth, and auth. It reload the user profile (on Firebase) to then update my userInstance on the app.

Firebase is set as offline capable at app level. The userInstance POJO is sync with Firebase at log and stop to be sync at unlog.

I have a special parameter, in my user profile that I can change (as an admin).

Every-time I update it on the Firebase console, it is replace by the previous value when the app start again.

How can this happen ?

BTW : 1/ Based on which mechanism are the data merged, if multiple client have different local values ?

Here is simpler code, where I tried to reproduce the error. :

MyApplication.java

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Firebase.setAndroidContext(this);
        Firebase.getDefaultConfig().setLogLevel(Logger.Level.DEBUG);
        Firebase.getDefaultConfig().setPersistenceEnabled(true);


    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    Firebase ref;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ref = new Firebase("https://millezim-test.firebaseIO.com").child("user");
        ref.keepSynced(true);

        Button br = (Button) findViewById(R.id.b_read);
        Button bs = (Button) findViewById(R.id.b_save);
        final TextView tv_r = (TextView) findViewById(R.id.tv_value_toread);
        final EditText tv_s = (EditText) findViewById(R.id.tv_value_tosave);

        user = new User();

        bs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!tv_s.getText().toString().equalsIgnoreCase(""))
                    user.setAge(Integer.valueOf(tv_s.getText().toString()));
                ref.setValue(user);
            }
        });

        br.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        User u = dataSnapshot.getValue(User.class);
                        if (u != null)
                            tv_r.setText(String.valueOf(u.getAge()));
                        else
                            tv_r.setText("Bad Value");
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                    }
                });
            }
        });

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User u = dataSnapshot.getValue(User.class);
                u.setCounter(u.getCounter() + 1);
                user = u;
                saveUser();
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

    public void saveUser() {
        ref.setValue(user);
    }
}

If you just change a value in the app, then the counter inc until the app stop : this is normal. But what is strand is the age pass from old to new then to old cyclically without stopping.

And I feel that behavior in my app, without the cyclic, as I do not have a counter, but I cannot change a parameter in the admin client, I always get back the previous value stored in the mobile.

I just Auth, then I update my UserInstance with AuthData + the User fetch from Firebase (probably the cached data), and then I save back the updated User under Firebase (As I may got new AuthData, and I normally get the latest User from Firebase)

2/ In this simple example, I saw that if I read the value at start, it fetch the data cached in the app. How can I force having online data ?

解决方案

The problem comes from the fact that you're using disk persistence with a single-value event listener. When you do:

ref.addListenerForSingleValueEvent(new ValueEventListener() {...

You're asking for a single value of that location (the user in your case). If Firebase has a value for that location in its local cache, it will fire for that value straight away.

The best way to solve this is to not use a single-value listener, but instead use a regular event listener. That way you will get two events: one for the cached version and one for the version that comes back from the server (if it is different).

The only alternative is to not use Firebase's disk persistence. Without that, there won't be a local cache for the data to be read from upon a restart.

There were a few discussions about this combination on the Firebase mailing list. Here's one: https://groups.google.com/forum/#!msg/firebase-talk/ptTtEyBDKls/XbNKD_K8CQAJ

这篇关于Firebase如何工作与共享数据同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆