swift - how to get the .child key in an array? -
i want take save of blocked user child string in code. simple might sound not able figure out.
static func block(myself: string, posteruid: string){ var blockeduserref = database.database().reference().child("users").child(myself).child("blockedusers").child(posteruid) blockeduserref.setvalue(true) var blokedarray:[string] = [] var handle:databasehandle! blockeduserref = database.database().reference() handle = blockeduserref.child("blockedusers").observe(.childadded, with:{ (datasnapshot) in if let item = datasnapshot.value as? string{ blokedarray.append(item) } }) }
i want take array of blocked users go through in loop , filter them showing.
ref.observesingleevent(of: .value, with: { (snapshot) in guard let snapshot = snapshot.children.allobjects as? [datasnapshot] else { return completion([]) } // 3 filter user array let users = snapshot .flatmap(user.init) .filter { $0.uid != currentuser.uid }
here firebase:
"users" : { "3ylvuda8ynsuvyutxv6cvoqpsjm1" : { "username" : "πΈππΈ" }, "4duy2hhtv5s7ssxtzybfip7jblz1" : { "username" : "love nyc" }, "58t0m2fxxhg6grt96vvbkmfhrko2" : { "blockedusers" : { "4duy2hhtv5s7ssxtzybfip7jblz1" : true, "sqarodpjuydcdv6dexteidzkare2" : true }, "username" : "istandwithyou" },
some new ways trying debugged , seems not saving onto array
///adedblock var blockeduserref = database.database().reference().child("users").child(currentuser.uid).child("blockedusers") var blokedarray:[string] = [] var handle:databasehandle! blockeduserref = database.database().reference() handle = blockeduserref.child("blockedusers").observe(.childadded, with:{ (datasnapshot) in if let item = datasnapshot.value as? string{ blokedarray.append(item) } }) user in blokedarray{ let users = snapshot .flatmap(user.init) .filter { $0.uid != user } }
this can done saving of child entries of blockedusers array hold strings.
to this, first create empty array like
mylist:[string] = []
then, need create reference firebase database like:
var ref: databasereference!
you need database handle navigate around database:
var handle:databasehandle!
now, need go viewdidload function , add code like:
ref = database.database().reference() handle = ref?.child("blockedusers").observe(.childadded, with:{ (datasnapshot) in if let item = datasnapshot.value as? string{ self.mylist.append(item) } })
this piece of code checking whether entry added child database "blockedusers", , that, adding whatever entered array.
now, have array filled whatever in child database, blockedusers. can use array in uitableview, or list in label.
hope helped!
Comments
Post a Comment