检索新的Firebase数据 - iOS Swift

时间:2022-10-16 21:25:46

检索新的Firebase数据 -  iOS Swift

I'm new to iOS programming. In my project, I have stored the data into the firebase database. I have to retrieve the data for all the "meanACC" values and store it into an array so that the value of that array would be [2,5,6,6]. Please help.

我是iOS编程的新手。在我的项目中,我已将数据存储到firebase数据库中。我必须检索所有“meanACC”值的数据并将其存储到一个数组中,以便该数组的值为[2,5,6,6]。请帮忙。

2 个解决方案

#1


2  

With your current structure you should be doing this loading all the data and iterating over the children to compare each one of the meanAcc.

使用您当前的结构,您应该这样做加载所有数据并迭代子进程来比较每个meanAcc。

ref.child("Data").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
    if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for child in result {
            //do your logic and validation here
            child.value["meanAcc"] as! String
        }
    } else {
        print("no results")
    }
}) { (error) in
   print(error.localizedDescription)
} 

Another option is to, whenever storing a new value under /Data, store the meanAcc value inside another branch MeanAccs. And here I'm assuming that the rest of your data is not relevant for your validations.

另一个选择是,每当在/ Data下存储新值时,将meanAcc值存储在另一个分支MeanAccs中。在这里,我假设您的其他数据与您的验证无关。

/MeanAccs
    /meanAccValue1 : true
    /meanAccValue2 : true

With this structure you can have an array of all the meanAcc without having to load the additional information under Data.

使用此结构,您可以拥有所有meanAcc的数组,而无需在Data下加载其他信息。

ref.child("MeanAccs").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
    if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for child in result {
            var meanAcc = child.key as! String
            print(meanAcc)
        }
    }
})

#2


0  

Just a standard query and add the values to an array?

只是标准查询并将值添加到数组?

    var myArray = [Int]()

    let dataRef = myRootRef.childByAppendingPath("Data")

    dataRef.queryOrderedByChild("meanAcc").observeEventType(.Value, withBlock: {
        snapshot in

        for child in snapshot.children {
            let val = child.value["meanAcc"] as! Int
            myArray.append(val)
        }
        print(myArray) //prints the array [2, 5, 6, 6]
    })

#1


2  

With your current structure you should be doing this loading all the data and iterating over the children to compare each one of the meanAcc.

使用您当前的结构,您应该这样做加载所有数据并迭代子进程来比较每个meanAcc。

ref.child("Data").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
    if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for child in result {
            //do your logic and validation here
            child.value["meanAcc"] as! String
        }
    } else {
        print("no results")
    }
}) { (error) in
   print(error.localizedDescription)
} 

Another option is to, whenever storing a new value under /Data, store the meanAcc value inside another branch MeanAccs. And here I'm assuming that the rest of your data is not relevant for your validations.

另一个选择是,每当在/ Data下存储新值时,将meanAcc值存储在另一个分支MeanAccs中。在这里,我假设您的其他数据与您的验证无关。

/MeanAccs
    /meanAccValue1 : true
    /meanAccValue2 : true

With this structure you can have an array of all the meanAcc without having to load the additional information under Data.

使用此结构,您可以拥有所有meanAcc的数组,而无需在Data下加载其他信息。

ref.child("MeanAccs").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
    if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for child in result {
            var meanAcc = child.key as! String
            print(meanAcc)
        }
    }
})

#2


0  

Just a standard query and add the values to an array?

只是标准查询并将值添加到数组?

    var myArray = [Int]()

    let dataRef = myRootRef.childByAppendingPath("Data")

    dataRef.queryOrderedByChild("meanAcc").observeEventType(.Value, withBlock: {
        snapshot in

        for child in snapshot.children {
            let val = child.value["meanAcc"] as! Int
            myArray.append(val)
        }
        print(myArray) //prints the array [2, 5, 6, 6]
    })