<tfoot id='tFYpj'></tfoot>
  • <legend id='tFYpj'><style id='tFYpj'><dir id='tFYpj'><q id='tFYpj'></q></dir></style></legend>
  • <small id='tFYpj'></small><noframes id='tFYpj'>

    <i id='tFYpj'><tr id='tFYpj'><dt id='tFYpj'><q id='tFYpj'><span id='tFYpj'><b id='tFYpj'><form id='tFYpj'><ins id='tFYpj'></ins><ul id='tFYpj'></ul><sub id='tFYpj'></sub></form><legend id='tFYpj'></legend><bdo id='tFYpj'><pre id='tFYpj'><center id='tFYpj'></center></pre></bdo></b><th id='tFYpj'></th></span></q></dt></tr></i><div id='tFYpj'><tfoot id='tFYpj'></tfoot><dl id='tFYpj'><fieldset id='tFYpj'></fieldset></dl></div>

        <bdo id='tFYpj'></bdo><ul id='tFYpj'></ul>

      1. 我想在执行 firebase 代码后返回布尔值

        时间:2023-10-04
          <bdo id='Gvcm1'></bdo><ul id='Gvcm1'></ul>

          • <legend id='Gvcm1'><style id='Gvcm1'><dir id='Gvcm1'><q id='Gvcm1'></q></dir></style></legend>

              <tfoot id='Gvcm1'></tfoot>

                1. <i id='Gvcm1'><tr id='Gvcm1'><dt id='Gvcm1'><q id='Gvcm1'><span id='Gvcm1'><b id='Gvcm1'><form id='Gvcm1'><ins id='Gvcm1'></ins><ul id='Gvcm1'></ul><sub id='Gvcm1'></sub></form><legend id='Gvcm1'></legend><bdo id='Gvcm1'><pre id='Gvcm1'><center id='Gvcm1'></center></pre></bdo></b><th id='Gvcm1'></th></span></q></dt></tr></i><div id='Gvcm1'><tfoot id='Gvcm1'></tfoot><dl id='Gvcm1'><fieldset id='Gvcm1'></fieldset></dl></div>
                    <tbody id='Gvcm1'></tbody>

                  <small id='Gvcm1'></small><noframes id='Gvcm1'>

                  本文介绍了我想在执行 firebase 代码后返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从 Firebase Google 检索数据.我正在检查我收到的数据是否过期.

                  I'm retrieving data from Firebase Google. I'm checking the data i received is expire or not.

                  func checkBought(movieName : String) -> Bool{
                  
                      var yesOrNo = false
                  
                      boughtRef.observeEventType(.Value, withBlock: { (snap) in
                  
                          if snap.value![movieName]! != nil {
                              if self.timestamp > snap.value![movieName]! as! Double {
                                  //expire
                                  print("expire")
                                  yesOrNo = false
                              } else {
                                  //not expire
                                  print("not expire")
                                  yesOrNo = true
                              }
                          } else {
                              //not bought yet
                              print("No movie")
                              yesOrNo = false
                  
                          }
                      })
                  
                      return yesOrNo
                  }
                  

                  现在,return 语句在执行 firebase 代码之前返回并更改 yesOrNo 的值.

                  Now, the return statement is returning before the firebase code is executed and change the value of yesOrNo.

                  推荐答案

                  经典之作:

                  您不能从包含异步任务的方法返回任何内容

                  你需要一个完成块,很简单

                  You need a completion block, simply

                  func checkBought(movieName : String, completion:(Bool) -> Void) {
                  
                      boughtRef.observeEventType(.Value, withBlock: { (snap) in
                  
                      if snap.value![movieName]! != nil {
                        if self.timestamp > snap.value![movieName]! as! Double {
                          //expire
                          print("expire")
                          completion(false)
                        } else {
                          //not expire
                          print("not expire")
                          completion(true)
                        }
                      } else {
                        //not bought yet
                        print("No movie")
                        completion(false)
                  
                      }
                    })
                  }
                  

                  或者更简单

                  func checkBought(movieName : String, completion:(Bool) -> Void) {
                    boughtRef.observeEventType(.Value, withBlock: { (snap) in
                      if let movieStamp = snap.value![movieName] as? Double where self.timestamp <= movieStamp {
                        //not expire
                        print("not expire")
                        completion(true)
                      } else {
                        // expire or not bought yet
                        print("expire or no movie")
                        completion(false)
                      }
                    })
                  }
                  

                  并用

                  checkBought("Foo") { flag in
                     print(flag)
                  }
                  

                  这篇关于我想在执行 firebase 代码后返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将参数传递给 AsyncTask,并返回结果 下一篇:访问超出范围 Swift 的变量

                  相关文章

                  <small id='1SwXO'></small><noframes id='1SwXO'>

                    <legend id='1SwXO'><style id='1SwXO'><dir id='1SwXO'><q id='1SwXO'></q></dir></style></legend>

                  1. <i id='1SwXO'><tr id='1SwXO'><dt id='1SwXO'><q id='1SwXO'><span id='1SwXO'><b id='1SwXO'><form id='1SwXO'><ins id='1SwXO'></ins><ul id='1SwXO'></ul><sub id='1SwXO'></sub></form><legend id='1SwXO'></legend><bdo id='1SwXO'><pre id='1SwXO'><center id='1SwXO'></center></pre></bdo></b><th id='1SwXO'></th></span></q></dt></tr></i><div id='1SwXO'><tfoot id='1SwXO'></tfoot><dl id='1SwXO'><fieldset id='1SwXO'></fieldset></dl></div>

                      • <bdo id='1SwXO'></bdo><ul id='1SwXO'></ul>
                    1. <tfoot id='1SwXO'></tfoot>