2017-09-13 62 views
0

我在我的應用程序中創建了一個名爲「刪除我的賬戶」的方法,並刪除了我創建的一些查詢的所有用戶資料。如何通過查詢從Firebase Storage Android中刪除圖像?

這是數據庫的情況:https://i.stack.imgur.com/eaUYh.jpg

正如你可以看到這個用戶發佈一書與自己的形象,猜測用戶發佈更多的書,我怎麼能刪除該用戶發佈的所有書籍的圖片?

這是我的代碼刪除:

private DatabaseReference mUserBook; 

private Query user_book; 

mUserBook = FirebaseDatabase.getInstance().getReference("Books"); 

user_book = mUserBook.orderByChild("uid").equalTo(currentUid); 

user_book.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 

      if(dataSnapshot.getValue() != null){ 

       for(DataSnapshot book_Snap : dataSnapshot.getChildren()){ 

        book_Snap.getRef().removeValue(); 
       } 
      } 

通過這種方式,將刪除所有數據,但不能圖書中的圖片,如果他張貼在這種情況下更多的書,我怎麼能解決呢?

回答

0

問題是您刪除了整本書。嘗試使用book_Snap.getRef().child('image').removeValue();。這樣它只會刪除圖像的孩子。

編輯:

要刪除存儲的書本身,我建議你使用Cloud Functions。刪除書籍的收聽者看起來是這樣的:

// Listens on removing a book 
exports.removeBookImageFromStorage = functions.database.ref('/Books/{bookId}/image/').onDelete(event => { 
    // Remove image from the Storage - you can get the imageUrl or the bookId from the event variable 
});` 
+0

如果我刪除孩子,那麼它不會刪除Storage Reference中的圖像本身嗎?我怎樣才能刪除它? – Nicco

+0

這就是對,我建議你爲此做一個CloudFunction。有人在數據庫中進行更改(創建,編輯或刪除)時可以使用CloudFunctions。您可以在[文檔](https://firebase.google.com/docs/functions/)中閱讀它。刪除書籍後,您可以將其刪除。我編輯了我的答案,所以你有一個暗示來製作這樣的聽衆。 –