2016-07-15 9 views
1

火力數據庫中有一個功能,我可以當什麼路徑我如何檢測一個特定的火力存儲路徑數據已經改變

firebase.database().ref('users/' + Auth.uid + '/profileImg').on('value', function(snapshot) { //do things when the data changed}); 

我想改變再次檢索數據,如果任何人知道,如果火力存儲不一樣的東西?例如,如果我上傳另一個配置文件圖片,我該如何檢索該imageUrl?

我知道我可以通過下面的方式檢索它,但由於我想檢測與上載位置不在同一控制器中的另一個控制器中的更改,因此此方法無效。

     uploadTask.on('state_changed', function (snapshot) { 
         // Observe state change events such as progress, pause, and resume 
         // See below for more detail 
        }, function (error) { 
         // Handle unsuccessful uploads 
        }, function() { 
         $scope.userProfile = uploadTask.snapshot.downloadURL; 
        }); 
       }, function (error) { 
        console.error(error); 
       }); 

謝謝!

回答

1

當文件發生更改時,Firebase存儲沒有內置功能來主動提醒客戶端。

通過將Firebase存儲與Firebase實時數據庫相結合,您可以輕鬆構建這些內容。請您在數據庫文件將downloadURL並添加lastModified時間戳:

images 
    $imageid 
    downloadUrl: "https://downloadUrl" 
    lastModified: 123873278327 

當你上傳/更新火力地堡存儲器中的圖像,更新數據庫將downloadURL /時間戳:

uploadTask.on('state_changed', function (snapshot) { 
    // Observe state change events such as progress, pause, and resume 
    // See below for more detail 
}, function (error) { 
    // Handle unsuccessful uploads 
}, function() { 
    $scope.userProfile = uploadTask.snapshot.downloadURL; 
    databaseRef.child('images').child(imageId).set({ 
     downloadUrl: uploadTask.snapshot.downloadURL, 
     lastModified: firebase.database.ServerValue.TIMESTAMP 
    }) 
}); 

現在您可以通過聆聽該圖像的數據庫位置知道圖像何時被修改:

databaseRef.child('images').child(imageId).on('value', function(snapshot) { 
    // take the downloadUrl from the snapshot and update the UI 
}); 
+0

謝謝!弗蘭克,我會試一試,但我認爲它會奏效。此外,只是想知道你是否知道這一點,即使實際內容沒有改變,downloadURL是否隨着時間/位置而改變?所以現在,每次我需要一個新的downloadURL而不是將它保存在數據庫中時,我應該將它保存在數據庫中嗎? –

+0

而且我相信時間戳是由firebase2支持的。以下是firebase3的代碼firebase.database.ServerValue.TIMESTAMP –

+0

修復了代碼。感謝那。我一直忘記ServerValue.TIMESTAMP的移動。 –

相關問題