查询不工作Firebase Swift

时间:2016-11-24 18:25:58

标签: ios swift firebase firebase-realtime-database

我有一个Firebase,其结构如下:

  • "邮编"
    • 12345
      • 1
        • " city":" test"
        • "陈述":" XX"
    • 98765
      • 2
        • " city":" test2"
        • "陈述":" AA"
      • 3
        • " city":" test3"
        • "陈述":" BB"

我试图通过用户的邮政编码查询我的数据库;但是,当我尝试查看它找到的快照时,它为空。这是我目前的代码:

      val colTicker = new TableColumn[Quote, String] {
      editable = true
      text = "Ticker"
      prefWidth = 80
      alignmentInParent = scalafx.geometry.Pos.Center
      cellValueFactory = {
        _.value.ticker
      }
      cellFactory = _ => new TextFieldTableCell[Quote, String](new DefaultStringConverter())
      onEditCommit = (evt: CellEditEvent[Quote, String]) => {
        val quote: Quote = evt.rowValue
        val newTickerVal: String = evt.newValue.toUpperCase()
        val oldTickerVal: String = evt.oldValue
        // is it a valid ticker and not a dupe or is it blank (erase old ticker)?
        if ((isValidTicker(newTickerVal) || newTickerVal.length == 0) && !symbolList.contains(newTickerVal)) {
          // lock in the new value on the screen
          quote.ticker.set(newTickerVal)
          // if the new value is not empty add it to symbol list
          if (newTickerVal.length > 0) {
            symbolList.append(newTickerVal)
          }
          // now delete the old value
          symbolList -= oldTickerVal
          // sort and add another blank line
          characters.sortWith(_.ticker.getValueSafe < _.ticker.getValueSafe)
          if (oldTickerVal.length < 1) characters += Quote()
          // now need to update the data file
          putListToFile(dataFile, symbolList.sorted)
        } else {
          // bad ticker so keep the old one and don't update file
          quote.ticker.set(oldTickerVal)
          evt.getTableView.getColumns.get(0).setVisible(false)
          evt.getTableView.getColumns.get(0).setVisible(true)
          println("bad ticker, exiting symbol list: " + symbolList)
        }
      }

    }
    val colLast = new TableColumn[Quote, String] {
      editable = false

      text = "Last"
      cellValueFactory = {
        _.value.last
      }

      prefWidth = 80
      alignmentInParent = scalafx.geometry.Pos.Center
    }
    val colChange = new TableColumn[Quote, String] {
      editable = false
      text = "Change"
      cellFactory = {
        _ =>
          new TableCell[Quote, String] {
              item.onChange { (_, _, newChange) =>
                if (newChange != null) {
                  if (newChange.toString.contains("+")) textFill = Color.Green
                  else textFill = Color.Red
                  text = newChange
                }
              }
          }
      }
      cellValueFactory = {
        _.value.change
      }
      prefWidth = 80
      alignmentInParent = scalafx.geometry.Pos.Center
    }

当我运行代码时,我明白了:

func firebaseSearch(zipCode: String) {

    let conditionRef = FIRDatabase.database().reference().child("zipcodes")

    let query = conditionRef.queryEqualToValue(zipCode)

    query.observeSingleEventOfType(.Value, withBlock: {snapshot in

        print(snapshot.value)

        for child in snapshot.children {
            print(child)
        }

    })

}

我的数据库有大约77,000个条目,所以我担心迭代所有条目并试图找到具有我的zipCode值的孩子将花费大量时间,所以我试图使用查询功能来查看它&# 39; s更快。感谢您的帮助,非常感谢!

1 个答案:

答案 0 :(得分:1)

您不需要对此进行查询,请使用.child(zipCode)代替.queryEqualToValue(zipCode)。这将返回预期的结果。

希望这会有所帮助!!