Мне нужно изменить профиль пользователя в моем приложении.Я сохраняю 2 изображения в Firebase Storage, и я должен сохранить новую информацию о пользователе в двух узлах базы данных Firebase. (Пользователи и 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;
}
Есть ли другой способ?Когда я использую приложение, данные сохраняются, но иногда каждая фотография загружается для каждого события, а иногда событие не обновляется.Мои извинения за мой английский.