检查是否接受了书面许可 [英] Check whether the writing permission was accepted or denied

查看:34
本文介绍了检查是否接受了书面许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用firebase为我的统一android应用实现交易系统.为此,我必须检查玩家是否为某笔交易按下了购买按钮,所选的交易在Firebase数据库中是否仍然可用.

I am currently trying to implement a trading system for my unity android app using firebase. For that I have to check, if a player pressed the buy button for one trade, if the selected trade is still available in the firebase database.

应该工作的方式是,要购买某种商品的玩家在对应的交易中进行检查,buyerId是否仍然为空,如果是,则在其中放置自己的ID.我已经设置了Firebase规则,因此BuyerId和BuyerName只能设置一次.

The way it is supposed to work is, that the player who wants to buy something checks in the corresponding trade, if the buyerId is still empty, and if so puts his own id in there. I've set up the firebase rules, so that the buyerId and buyerName can only be set once.

现在这就是我要使用的代码:

So that's now the code I was trying to use:

if (!reference.Child("trading").Child("trade|" + currentTrade.date).Child("buyerId").SetValueAsync(userId).IsCanceled)
{
    Debug.Log("trade should be successfull");
}
else
{
    Debug.Log("trade should have failed");
}

现在的问题是,buyerId是否为空无关紧要,统一控制台总是打印出来,交易成功.但实际上我确实在控制台中收到logWarning,该权限被拒绝.现在的问题是,如果已经设置了BuyerId,如何正确检测呢?

Well the problem is now, that it doesn't matter if the buyerId is empty or not, the unity console always prints out, that the trade was succesfully. But I do actually get a logWarning in the console, that the permission was denied. My question is now, how do I detect it properly if the buyerId was already set?

我的数据库结构:

{
  "currentVersion" : {
    "FileSize" : 58010,
    "ReleaseDate" : "01.08.19",
    "Version" : "1.10.7.9"
  },
  "trading" : {
    "trade|1570361586" : {
      "amount" : 5,
      "buyerId" : "",
      "buyerName" : "",
      "date" : 1570361586,
      "item" : 7,
      "price" : 6,
      "sellerId" : "1234",
      "sellerName" : "testA"
    },
    "trade|1570363226" : {
      "amount" : 1,
      "buyerId" : "9876",
      "buyerName" : "testB",
      "date" : 1570363226,
      "item" : 7,
      "price" : 1,
      "sellerId" : "1234",
      "sellerName" : "testA"
    },
    "trade|1570467885" : {
      "amount" : 1,
      "buyerId" : "4545",
      "buyerName" : "testC",
      "date" : 1570467885,
      "item" : 7,
      "price" : 2,
      "sellerId" : "1234",
      "sellerName" : "testA"
    }
  }
}

我的数据库规则:

{
    "rules": {
    "currentVersion" : {
        ".write" : false,
        ".read" : true     
    },
    "trading" : {
      ".read" : true,
      "$trade" : {
        ".write" : "!data.exists()",
        "buyerName" : {
          ".write" : "data.val() == '' && newData.val() != ''"
        },
        "buyerId" : {
          ".write" : "data.val() == '' && newData.val() != ''"
        },
        "sellerName" : {
          ".write" : false
        },
        "sellerId" : {
          ".write" : false
        },
        "amount" : {
          ".write" : false
        },
        "item" : {
          ".write" : false
        },
        "price" : {
          ".write" : false
        }

      }
    }
  }
} 

推荐答案

这对我来说很奇怪:

if (!reference.Child("trading").Child("trade|" + currentTrade.date).Child("buyerId")
  .SetValueAsync(userId).IsCanceled)

由于您正在调用 SetValueAsync ,因此该值将异步设置为数据库.调用 SetValueAsync 后, IsCanceled 出现 true 的机会非常小.

Since you're calling SetValueAsync, the value is set to the database asynchronously. The chances of IsCanceled being true right after your call to SetValueAsync are incredibly small.

作为知道何时提交数据的文档解释,您将要等到从 SetValueAsync 返回的 Task 完成.

As the documentation on knowing when your data is committed explains, you'll want to wait until the Task that is returned from SetValueAsync is completed.

类似这样的东西:

reference.Child("trading").Child("trade|" + currentTrade.date).Child("buyerId")
  .SetValueAsync(userId).).ContinueWith(task => {
        if (task.IsFaulted)
        {
            // Handle the error...
        }

这篇关于检查是否接受了书面许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆