Retrieve child key one by one and display in label swift - swift

I'm trying to retrieve the child in Firebase and print them EACH in a label but when I'm retrieving the key its printing all at the same time. I'm still learning on the go so here's what i tried:
func retrieve(){
let userId = Auth.auth().currentUser?.uid
let intakeRef = Database.database().reference().child("Users").child(userId!).child("Intake")
intakeRef.observeSingleEvent(of: .value, with: { (snapshot) in
if let intake = snapshot.value{
self.ref.child("Attendance").child("Checkin").child(intake as! String).child(userId!).child("BM050-3-3-IMNPD").observe(.value, with: { (snapshot) in
for child in snapshot.children{
let key = (child as AnyObject).key as String
print(key)
label.text = key
}
})
}
}) { (Error) in
print("unable to retrieve name from nameRef")
}
}
The code I tried like i said is printing all at the same time. But i would like to print each one in a separate label. Any help would really be appreciated!

You'll of course need to create a tableViewController and link it up to a TableView and assign your cell its own identifier, but this is a good outline of what you'd need to do in your own tableViewController:
class tableViewController: UITableViewController {
var dataArray = [String]()
var ref = Database.database().reference() //You didn't include your delcaration of ref in your code, so don't use this, just use the one you were already using.
override func viewDidLoad() {
super.viewDidLoad()
retrieve()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let data = dataArray[indexPath.row]
cell.textLabel?.text = data
return cell
}
func retrieve(){
let userId = Auth.auth().currentUser?.uid
let intakeRef = Database.database().reference().child("Users").child(userId!).child("Intake")
intakeRef.observeSingleEvent(of: .value, with: { (snapshot) in
if let intake = snapshot.value{
self.ref.child("Attendance").child("Checkin").child(intake as! String).child(userId!).child("BM050-3-3-IMNPD").observe(.value, with: { (snapshot) in
for child in snapshot.children{
let key = (child as AnyObject).key as String
self.dataArray.append(key)
}
})
self.tableView.reloadData()
}
}) { (Error) in
print("unable to retrieve name from nameRef")
}
}
}
Again you'll need to create your own UItableView, link it up to a UITableViewController, assign your own cell reuse identifier and make sure that cellForRowAt has the correct reuse identifier in it, and you'll need to change the ref variable in my provided code to the ref that you need to use.

Related

Displaying Firebase data in a tableview

I am trying to retrieve information stored in a Firebase Database, but my tableview is not displaying the information. I have tried using print functions to see if the data is being retrieved, and it shows that this is the case, but the tableview shows up as blank when I run the simulator.
I am using Xcode 11 and every tutorial that I have looked at is not working for some reason.
Here is my code:
import UIKit
import Firebase
import FirebaseDatabase
import SwiftKeychainWrapper
import FirebaseAuth
class FeedVC: UITableViewController {
var currentUserImageUrl: String!
var posts = [postStruct]()
var selectedPost: Post!
override func viewDidLoad() {
super.viewDidLoad()
getUsersData()
getPosts()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getUsersData(){
guard let userID = Auth.auth().currentUser?.uid else { return }
Database.database().reference().child("users").child(userID).observeSingleEvent(of: .value) { (snapshot) in
if let postDict = snapshot.value as? [String : AnyObject] {
self.tableView.reloadData()
}
}
}
struct postStruct {
let firstName : String!
let lastName : String!
}
func getPosts() {
let databaseRef = Database.database().reference()
databaseRef.child("profiles").queryOrderedByKey().observeSingleEvent(of: .childAdded, with: {
snapshot in
let firstName = (snapshot.value as? NSDictionary)!["profileForename"] as? String
let lastName = (snapshot.value as? NSDictionary
)!["profileSurname"] as? String
print(firstName)
self.posts.append(postStruct(firstName: firstName, lastName: lastName))
print(self.posts)
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell else { return UITableViewCell() }
let nameLabel = cell.viewWithTag(1) as! UILabel
nameLabel.text = posts[indexPath.row].firstName
return cell
}
}
Any help would be much appreciated!
Update: Since PostCell is created in the storyboard within the table view it's registered and dequeued successfully. So the issue is being narrowed down to the label with tag 1. Try creating an #IBOutlet for the label and use that to set the text of UILabel.
Then in cellForRowAt:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell else { return UITableViewCell() }
cell.firstNameLabel.text = posts[indexPath.row].firstName
return cell
}
Previous: You seem to have forgotten to register the PostCell.
override func viewDidLoad() {
super.viewDidLoad()
//...
tableView.register(PostCell.self, forCellReuseIdentifier: "PostCell")
}
Note: If you've created PostCell in Xib use nib registry method.
Update: If you want to register with Nib method use:
tableView.register(UINib(nibName: <#T##String#>, bundle: nil), forCellReuseIdentifier: "PostCell") // provide the xib file name at the placeholder

Is it possible to display the 'name' of a firebase node as well as its value in Xcode?

I am trying to show User ID's in my app, however the UID's are very random and next to each one is a name, is it possible to show both the name of the nodes as well as its value?
My firebase database
I want to display the Name "Andy" as well as the UID "uLUnOelxABYl3lCtLz2Of5Yfnvc2"
I have set it up so that the App receives the values from my Firebase.
Here is my code:
class TestViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = list[indexPath.row]
return cell!
}
#IBOutlet weak var tableView: UITableView!
var ref: DatabaseReference!
var list = [String] ()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
if FirebaseApp.app() == nil {
FirebaseApp.configure() }
ref = Database.database().reference()
ref?.child("users").observe(.childAdded, with: { (snapshot) in
let list1 = snapshot.value as? String
if let actualList = list1 {
self.list.append(actualList)
self.tableView.reloadData()
}
}
)
}
}
Here is what I get back. However I want to show is a list of the names with these values next to it, like this (photoshop)
results
PHOTOSHOP RESUTLS
Regarding to documentation FIRDataSnapshot has property key, so I think you can use it for matching with values, e.g:
/// e.g. use dictionary instead of Array
//var list = [String] ()
var list = [AnyHashable: String]()
...
ref?.child("users").observe(.childAdded, with: { (snapshot) in
let key = snapshot.key as? Hashable
let value = snapshot.value as? String
if let actualValue = value, let actualKey = key {
self.list[key] = actualValue
self.tableView.reloadData()
}
}

How to read data from firebase, block with doesn't work?

I try to read data from firebase. I've made observeSingleEvent, but block "with" not works, why?
I try to debug and I notice that block with doesn't work.
userID has correct ID
and reference also correct
var ref: DatabaseReference!
var snapData: NSDictionary?
var nameString = [String]()
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
loadData()
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "indentifire")
view.addSubview(table)
// Do any additional setup after loading the view.
}
// ---------------------------------------------------
//loading data from FireBase
func loadData() {
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
self.snapData = snapshot.value as? NSDictionary
})
}
// delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var temp = 0
for (_,val) in snapData! {
if val as? String == "false" {
temp += 1
nameString.append(val as! String)
}
}
return temp
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "indentifire", for: indexPath)
cell.textLabel!.text = nameString[indexPath.row]
return cell
}```
this is my database
![photo](https://imgur.com/a/0UzOPJ7
The database operations work asynchronously. Map the data in loadData and reload the table view
func loadData() {
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
if let snapData = snapshot.value as? [String:Any] {
self.nameString = snapData.values.compactMap {$0 as? String}
DispatchQueue.main.async {
self.table.reloadData()
}
}
})
}
And in numberOfRowsInSection just return the number of items in nameString
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameString.count
}

Display user info from Firebase

I want to read and display user info from Firebase.
Here is how far I've got:
class UserInfoViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
var ref: DatabaseReference!
private var gotName = [""]
#IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gotName.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "UserInfoCell") as! UserInfoTableViewCell
cell.nameLabel.text = gotName[indexPath.row]
return cell
}
override func viewDidLoad() {
ref = Database.database().reference()
let user = Auth.auth().currentUser!.uid
ref?.child("users").child(user).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDict = snapshot.value as? [String: Any],
let name = userDict["Name"] as? String else {
return
}
//Declare variables for use
self.gotName = [name]
})
}
}
Firebase structure:
-users
-----5DzurQyzyIbXfFCbAxc4ynwizYJ2
---------John Doe
I want the current user to be displayed in the nameLabel
You have written the code correctly.
but you must reload your table view after downloading the data from firebase.
Just write following code after self.gotName = [name] in your firebase observer.
DispatchQueue.main.async {
self.tableView.reloadData()
}
Hope this helps

Data inside Firebase Database read twice with an observeSingleEvent

It's been now several hours I'm trying to dig Internet forums to try to find a solution to my problem and I can't figure out what's happening so I hope someone here can help)
I have a swift app and on one of my viewcontrollers, what I would want to do is to retrieve data from Firebase database and display them on my tableview.
Here you can see how my data are organized. Firebase org of data
To get the number of rows in my tableview, I put this into the viewWillAppear section
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Count the number contacts to get the correct number of lines in the table
let ref_table = ref.child("users").child((user?.uid)!).child("emergencyContacts")
print("Starting observing")
ref_table.observe(.value, with: { (snapshot: DataSnapshot!) in
print("Got snapshot")
print(snapshot.childrenCount)
let count = snapshot.childrenCount
self.numberOfContact = Int(count)
self.UI_contactsTableView.reloadData()
ref_table.removeAllObservers()
})
print("Returning count")
}
Then, to get the number of lines, I have this
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Number of contact TV: \(numberOfContact)")
return numberOfContact
}
And to fill the cells of my tableview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contact-cell") as! EmergencyContactCell
print("value of numberOfContact \(numberOfContact)")
ref.child("users").child((user?.uid)!).child("emergencyContacts").observeSingleEvent(of: .value, with: { (snapshot: DataSnapshot) in
print("snapshot value \(snapshot.value!)")
for rest in snapshot.children.allObjects as! [DataSnapshot] {
guard let restDict = rest.value as? [String:Any] else { continue }
let firstName = restDict["contact_firstName"] as? String ?? ""
cell.UI_fullName.text = "\(firstName)"
}
})
return cell
}
And I think this is where it gets bad because in the console, whatever I do, I constantly have 2 sets of data displayed so my cells are always filled with the same name as you can see here.
This is also a capture of my console after program execution.
Starting observing
Returning count
Number of contact TV: 0
Number of contact TV: 0
Number of contact TV: 0
Got snapshot
2
Number of contact TV: 2
value of numberOfContact 2
value of numberOfContact 2
snapshot value {
1ArkUu6pMPZPgf3pGFAvvxHiTPFaf5Cl = {
"contact_firstName" = Kate;
"contact_lastName" = Bell;
"contact_phoneNumber" = "(555) 564-8583";
};
s8haDrYYAT9Y12ZnmAfE87pDyjDZGwjx = {
"contact_firstName" = Daniel;
"contact_lastName" = Higgins;
"contact_phoneNumber" = "555-478-7672";
};
}
snapshot value {
1ArkUu6pMPZPgf3pGFAvvxHiTPFaf5Cl = {
"contact_firstName" = Kate;
"contact_lastName" = Bell;
"contact_phoneNumber" = "(555) 564-8583";
};
s8haDrYYAT9Y12ZnmAfE87pDyjDZGwjx = {
"contact_firstName" = Daniel;
"contact_lastName" = Higgins;
"contact_phoneNumber" = "555-478-7672";
};
}
Thanks in advance for your help:)
There is a an error in your cellForRowAtIndex Path method.
`ref.child("users").child((user?.uid)!).child("emergencyContacts").observeSingleEvent(of: .value, with: { (snapshot: DataSnapshot) in'
The above line returns all the emergency contacts each time the cellForRowAtIndexPath method is called.
In your for-loop you then always loop through and pick the last value.
You would need to extract the value for the particular index path.
I figured it out, I tried to have a total different reasoning and it worked. I actually used a handler to manage all this. I post my code here, it could help people in the same trouble as I was couple of hours ago.
var emergencyContactList = [EmergencyContact]()
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
fetchUsers()
}
func fetchUsers() {
refHandle = ref.child("users").child((user?.uid)!).child("emergencyContacts").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String:AnyObject] {
print(dictionary)
let emergencyContact = EmergencyContact()
emergencyContact.setValuesForKeys(dictionary)
self.emergencyContactList.append(emergencyContact)
DispatchQueue.main.async {
self.UI_contactsTableView.reloadData()
}
}
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return emergencyContactList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contact-cell") as! EmergencyContactCell
let _firstName = emergencyContactList[indexPath.row].contact_firstName ?? ""
let _lastName = emergencyContactList[indexPath.row].contact_lastName ?? ""
let _phoneNumber = emergencyContactList[indexPath.row].contact_phoneNumber ?? ""
cell.UI_fullName.text = "\(_firstName) \(_lastName)"
cell.UI_phoneNumber.text = "\(_phoneNumber)"
return cell
}
And I have a swift file apart EmergencyContact.swift
import Foundation
class EmergencyContact: NSObject {
#objc var contact_firstName:String?
#objc var contact_lastName:String?
#objc var contact_phoneNumber:String?
}
Now it works like a charm!
Thanks TheAppMentor for having taken the time to answer me :)