简体   繁体   中英

Receiving a Firebase snapshot from a child with an array SWIFT

So I am currently trying to take data from my Firebase database and set it as its own variable, but the child for each chart is a specific date and time (yy.mm.dd.hms). So i have an array storing all the dates I need, but i cant reference them when calling my snapshot. I've tried these two methods which throw the error "(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''"

var postCollection = [170802120618, 170802101427] //yy.mm.dd.hh.mm.ss   

ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath.row]).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

and

var postCollection = [170802120618, 170802101427]  //yy.mm.dd.hh.mm.ss  
let selection = self.postCollection[indexPath.row]
ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

And the Database chart being roughly:

FIR{
    users{
        uid{
            username: UserName
            Posts{
                170802120618{
                    First Name: first
                }
            }
        }
    }
}

Right. You want the child key to be an autogenerated hashvalue. You can create these by using childByAutoId() . Also if I were you, I would just store that dates as string and parse those as needed. Something below would be an example:

Posts {
   -Kebfdajksthm {
      first_name: "first",
      post_date: "yymmddhhmmss"
   }
}

Try This

var post = [String]()
ref.observe(.value, with: { (snapshot) in
            for item in snapshot.children{
                self.post.append((item as AnyObject).key)
        }
    })

Then you print "post" and you will get ["170802120618", "170802101427"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM