简体   繁体   English

Firebase 实时数据库 Hashmap 覆盖数据

[英]Firebase realtime Database Hashmap overwrites data

In short, when I click on the marker it must register the user id of the user who clicked on "Partecipanti".简而言之,当我单击标记时,它必须注册单击“Partecipanti”的用户的用户 ID。 The problem is that if the first user is saved regularly when a second user clicks, this overwrites the first one.问题是如果第一个用户在第二个用户点击时定期保存,这会覆盖第一个用户。 How can I solve it?我该如何解决?

 @Override
public void onInfoWindowClick(@NonNull Marker marker) {

    LatLng latLon = marker.getPosition();

    for(Incontro incontro : Incontri) {
        if (latLon.equals(incontro.getLatlng())) {

            if(){
                Toast.makeText(Cercaincontro.this, "You are already attending this event", Toast.LENGTH_SHORT).show();
            }
            else {

                DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Incontro").child(incontro.getIncontroidId());
                HashMap<String, Object> hashMap1 = new HashMap<>();
                String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                hashMap1.put("userid", firebaseUser.getUid());

                reference.child("Partecipanti").child(uid).setValue(true);

            }

        }
    }

}

While @AliasCartellano's solution to pushing data into the Realtime Database will work, please note a pushed ID it's not the best option to identify a user.虽然@AliasCartellano 将数据推送到实时数据库的解决方案可行,但请注意推送的 ID,它不是识别用户的最佳选择。 Why?为什么? Because every time you call push() , another unique key is generated.因为每次调用push()时,都会生成另一个唯一键。

Since I see in your screenshot that you store some long IDs, other than the pushed IDs, the ones that start with - (minus), I assume you're using authentication.由于我在您的屏幕截图中看到您存储了一些长 ID,而不是推送的 ID,即以- (减号)开头的 ID,因此我假设您正在使用身份验证。 That being said, a more recommended way to store user data would be like this:话虽如此,更推荐的存储用户数据的方法是这样的:

Firebase-root
  |
  --- users
       |
       --- $uid
            |
            --- //user data

And to add data to such a structure, you should use the following lines of code:要将数据添加到这样的结构中,您应该使用以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
reference.child("Partecipanti").child(uid).setValue(hashMap1);

This means, that each user will write data under a separate location since each UID is unique.这意味着,每个用户都将在单独的位置写入数据,因为每个 UID 都是唯一的。 So in this way, you cannot overwrite the data.因此,通过这种方式,您无法覆盖数据。

Edit:编辑:

Firebase-root
  |
  --- users
       |
       --- $uid: true
       |
       --- $uid: true

And in code:在代码中:

reference.child("Partecipanti").child(uid).setValue(true);

Edit2:编辑2:

To be able to write an object that looks like this, please use the following lines code:为了能够编写如下所示的 object,请使用以下行代码:

Map<String, Object> data = new HashMap<>();
data.put("addressorganizz", "messina");
data.put("cittaorg", "messin");
//As as much data as you need.
reference.child("Partecipanti").child(uid).setValue(data);

Edit3:编辑3:

//Data
Map<String, Object> data = new HashMap<>();
data.put("addressorganizz", "messina");
data.put("cittaorg", "messin");

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid(); 
DatabaseReference reference = FirebaseDatabase.getInstance()
    .getReference("Incontro")
    .child(incontro.getIncontroidId())
    .child(uid);  
reference.setValue(data);

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

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