我需要在我的应用中修改用户的个人资料。我在Firebase存储中保存了2个图像,我必须在Firebase数据库的两个节点中保存用户的新信息。(Users and Events-eventsID-author) 这是我的Firebase控制台:
Firebase-root
|
--- Users
|
--- uid
|
--- name: "name"
|
--- city: "Campobasso"
|
--- url_photo_small: "urlStorage"
|
--- url_photo_large: "urlStorage"
--- Events
|
--- eventsID
|
--- author
|
--- uid
|
--- name: "name"
|
--- city: "Campobasso"
|
--- url_photo_small: "urlStorage"
|
--- url_photo_large: "urlStorage"
--- UsersEvents
|
--- uid
|
--- eventsID:true
这是我要更新的代码
private ArrayList<String> eventi = new ArrayList<>();
...
rootRef = FirebaseDatabase.getInstance().getReference();
//I load in eventi the events that contains the user's data and that I must update.
rootRef.child("UsersEvents").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
eventi.add(data.getKey());
Log.d(TAG,data.getKey());
updateEventForAuthor(eventi);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
final Map<String,Object> userDataEvents= new HashMap<>();
//I use userDataEvents for update author in events
userDataEvents.put("uid",uid);
userDataEvents.put("name",name.getText().toString());
userDataEvents.put("city",citta.getText().toString());
//firebaseUpdate is the data to save in Users node
firebaseUpdate.put("/Users/"+uid+"/uid",uid);
firebaseUpdate.put("/Users/"+uid+"/name",name.getText().toString());
firebaseUpdate.put("/Users/"+uid+"/city",citta.getText().toString());
StorageReference path = storeRef.child(uid).child("pics").child("profile");
//I save the first image, take its url and I save the data in Events and Users. path.child("small"+uid+".jpg").putFile(uri_image_small).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
url_image = task.getResult().getDownloadUrl().toString();
userDataEvents.put("url_photo_small",url_image);
firebaseUpdate.put("/Users/"+uid+"/url_photo_small",url_image);
//I save user's data in every events associated to him
if(eventi.size()!= 0){
for(int i=0;i<eventi.size();i++){
firebaseUpdate.put("/Events/"+eventi.get(i)+"/author",userDataEvents);
}
}
//Update user's data
rootRef.updateChildren(firebaseUpdate);
Toast.makeText(getApplicationContext(),"Upload eseguito small..",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Errore durante Upload Image",Toast.LENGTH_LONG).show();
}
}
});
//I save the second image and update data in Users and in Events
path.child("large"+uid+".jpg").putFile(uri_image_large).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
url_image = task.getResult().getDownloadUrl().toString();
userDataEvents.put("url_photo_large",url_image);
firebaseUpdate.put("/Users/"+uid+"/url_photo_large",url_image);
if(eventi.size()!= 0){
for(int i=0;i<eventi.size();i++){
firebaseUpdate.put("/Events/"+eventi.get(i)+"/author",userDataEvents);
}
}
//Aggiorna il nodo user
rootRef.updateChildren(firebaseUpdate);
Toast.makeText(getApplicationContext(),"Upload eseguito small..",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Errore durante Upload Image",Toast.LENGTH_LONG).show();
}
}
});
其中
private void updateEventForAuthor(ArrayList<String> eventi) {
this.eventi = eventi;
}
还有其他方法吗?当我使用应用程序时,数据会被保存,但有时每张照片都会加载到每个事件上,有时候事件没有更新。我为我的英语道歉。
答案 0 :(得分:0)
在onChildChanged
方法中使用 addChildEventListener ,您可以检查更新后的事件
答案 1 :(得分:0)
我尝试了很多方法。问题是照片保留在缓存中。我找到了答案here。