Errors trying to fetch users from firebase [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to fetch users form my Firebase database, display their names on the UITableView and be able to search through them. For some reason, I am seeing two errors on this function to fetch users.
ERROR: Value of type 'User' has no member 'append' at the line 'self.users.append(user)'
ERROR: No 'dispatch_get_main_queue' candidates produce the expected contextual result type '(DispatchQueue, () -> Void)' at the line 'dispatch_async(dispatch_get_main_queue)'
If you know any way to fix this please let me know. I've been stuck on this for a while now and can't seem to get the users to display. Thanks.
func fetchUser() {
Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
//will crash if your class properties don't exactly match up with firebase dictionary keys
user.setValuesForKeys(dictionary)
self.users.append(user)
//this will crash because of background thread, so use dispatch async to fix
dispatch_async(dispatch_get_main_queue)
self.tableView.reloadData()
print(user.name!, user.email!)

Second error is because that's not a proper syntax for running something on main thread. Should be
DispatchQueue.main.async {
self.tableView.reloadData()
}
As for the first error, I can only speculate (since you didn't provide how self.users is defined. But I suspect you defined it as var users: User, instead of array. So make sure you have:
var users: [User]
or with initialization:
var users = [User]()

Related

for in loop keeps running firebase database swift [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I have the following code to retreive data from my firebase database:
ref = Database.database().reference().child("data").child("someData")
ref.observeSingleEvent(of: .value) { (snapshot) in
for child in snapshot.children {
let someSnap = child as! DataSnapshot
self.someArray.append(someSnap.key)
self.collectionView?.reloadData()
}
This code succesfully appends the 4 string elements i have in my database to the array in my viewcontroller class, however it does so over and over again. The array keeps growing with hundreds of strings of the same 4 items over and over. How can I get only the 4 items I want?
It seems that you call the function many times , so make sure to clear it
self.someArray.removeAll()
for child in snapshot.children {
let someSnap = child as! DataSnapshot
self.someArray.append(someSnap.key)
}
self.collectionView?.reloadData()
As from your question, you are calling function many times, when you call function again remove all elements from array or clear array and reload your collection view so it does not add same items again. As updated your code:
ref = Database.database().reference().child("data").child("someData")
ref.observeSingleEvent(of: .value) { (snapshot) in
self.someArray.removeAll()
for child in snapshot.children {
let someSnap = child as! DataSnapshot
self.someArray.append(someSnap.key)
}
self.collectionView?.reloadData()
}
Since I don't have access to the database I'd suggest you use the debugger over the for loop and trace it step by step and checking for the values in each iteration to see what exactly you're appending to the array

i have simple firebase code error in the new xcode update [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
this method was writing with swift 3 in swift 4 I get error and I don't know where is the error
var CURRENT_USER: User? {
if let currentUser = Auth.auth().currentUser {
return currentUser
}
return nil
}
The error might have something to do with the optional type since it states that:
Cannot convert return expression of type 'User' to return type 'User?'
A good place to start would be to return an optional value by changing your code to
var CURRENT_USER: User? {
return Auth.auth().currentUser
}

Take value of the child of a child of a child in Firebase [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to observe this value:
With this code:
But this give me error. I tried many times with other database methods like make value1 in a single child divided by value 2.
Try this
ref2.child("keywords").child("hello").child("hello").observe(.value, with: { snapshot in
guard
let dict = snapshot.value as? [String:Any],
let value1 = dict["value1"] as? Int
else { print("Error"); return }
print(value1)
})
Next time paste your code as text, not as an image

using swift 3 how to retrive data inside another data from firebase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to fetch data in a specific data such in age_range inside the 'min' which equal to 21
although 'total' inside 'summary' that is inside friends.
i only success fetching normal data such as name and gender ....
your help would be much appreciable
If I understand your question correctly you want to retrieve data from the child of a child.
ref?.child("users").child(uid).child("youCanAddAsManyOfTheseAsYouWishToAccessChildrenOfChildren").observeSingleEvent(of: .value, with: { (snapshot) in
//you can either access children in the line above or by:
snapshot.childSnapshot(forPath: "childName").value
})
If this is not the answer to your question just comment below.
edit: Here is an exact example for you.
ref?.child("users").child(uid).child("FB result").child("age_range").child("min").observeSingleEvent(of: .value, with: { (snapshot) in
//you can either access children in the line above or by:
print(snapshot.value)
})
Swift 3 & Xcode 8.3.2
I hope this code will solve your problem
// Create constant of baseURL (Make your life easier)
let baseURl = Database.database().reference()
// create reference that u want to fetch
let refToFetch = baseURl.child("users").child("uid").child("FB result").child("age_range")
// fetch the value
refToFetch.observeSingleEvent(of: .value, with: { (snapshot) in
// do something with value for key "min"
print(snapshot.value(forKey: "min") ?? "no value")
if let value: Int = snapshot.value(forKey: "min") as? Int {
// You got the value
print("value of min is: \(value)")
}
}) { (error) in
print(error, "Failed to fetch value")
}

Could not cast value of type(NSSingleEntryDictionary) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am getting the below error. My app is crashing for this reason.
Could not cast value of type '__NSSingleEntryDictionaryI' (0x1015f8210) to 'NSMutableDictionary'
All I am doing is :
var tempDict = self.arrayData.object(at: indexPath.row) as!
NSMutableDictionary
I have checked the others answers on stack but unfortunately they were not helpful for me.
Can anybody suggest me why this is happening?
Any help would be higgle appreciated!!
Simply do not use mutable Foundation collection types (NSMutable..) at all in Swift. They are not related to the Swift counterparts and you cannot cast an collection object to NSMutable...
Declare arrayData as native Swift array of dictionaries
var arrayData = [[String:Any]]()
and change the line to get the dictionary to
var tempDict = self.arrayData[indexPath.row]
Less code, no type cast and tempDict is mutable with the var keyword.
Dictionary denotes with {} and Array denotes with [] // In printed response you may have array with ()
So, your tempDict part is Array of Dictionary...You have to parse it like
var tempDict = self.arrayData.object(at: indexPath.row) as! [[String : Any]]
although please not use force unwrap .. either use if let or guard statement
if let tempDict = self.arrayData.object(at: indexPath.row) as! [[String : Any]]
{
// do something
}
else
{
// catch the error
}

Resources