簡體   English   中英

從 Firebase 實時數據庫中獲取價值 (Qt c++)

[英]Getting Value from Firebase Real-time Database (Qt c++)

我正在使用 Qt 為 Android 開發一個應用程序,此時我試圖從我的 Firebase 實時數據庫中獲取一個值,但我一直為firebase::kFutureStatusPending獲取0 ( false )。 我可以通過在我的數據庫上使用SetValue()來設置值,並且我已經使用dbref.Child(user->uid()).Child("Nickname").url()檢查了url ,它是正確的。 這是我與這部分相關的一段代碼,最后我還包含了 JSON 結構:

firebase::database::Database *database=firebase::database::Database::GetInstance(_app);
dbref = database->GetReferenceFromUrl("https://***/");

firebase::Future<firebase::database::DataSnapshot> result =
    dbref.Child(user->uid()).Child("Nickname").GetValue();


if (result.status() != firebase::kFutureStatusPending) {
  if (result.status() != firebase::kFutureStatusComplete) {
    qDebug() <<"ERROR: GetValue() returned an invalid result.";

  } else if (result.error() != firebase::database::kErrorNone) {
    qDebug() << result.error_message();

  } else {
    firebase::database::DataSnapshot snapshot = *result.result();
    qDebug() << "snapshot available" ;
  }
}

else {
    qDebug() << "results are still pending";
}

如何在我的Qt Android app獲取Nickname child(在本例中為Raad )的值?

這是JSON文件內容:

{
  "YQEa5KquWgOiPHfD7SLSgU92mTH2" : {
    "Email Address" : "shariatraad@gmail.com",
    "Nickname" : "Raad"
  }
}

在從文檔中復制代碼時,您似乎錯過了這條評論:

// In the game loop that polls for the result...

if (result.status() != firebase::kFutureStatusPending) {
  if (result.status() != firebase::kFutureStatusComplete) {

由於數據是異步加載的,因此result.status()不會以正確的方式完成。 因此,您需要在游戲循環或其他重復運行的地方進行此檢查。

另外,您也可以使用Future.onCompletion如圖所示在這里

 // Or, set an OnCompletion callback, which accepts a C++11 lambda or // function pointer. You can pass your own user data to the callback. In // most cases, the callback will be running in a different thread, so take // care to make sure your code is thread-safe. future.OnCompletion([](const Future< SampleResultType >& completed_future, void* user_data) { // We are probably in a different thread right now. if (completed_future.error() == 0) { DoSomethingWithResultData(completed_future.result()); } else { LogMessage("Error %d: %s", completed_future.error(), completed_future.error_message()); } }, user_data);

但在這種情況下,確保您的程序( main部分)在檢索數據之前不會退出也很重要,否則您將永遠看不到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM