in Education by
I am trying to intitialize my Collection View Model which holds a list of [Users] with a firebase snap that holds data. Here is the Collection View Model code: typealias JSON = [String:Any] class HomeDataSource: Datasource { var users: [User] init?(with snapshot:DataSnapshot) { var users = [User]() guard let snapDict = snapshot.value as? [String: [String:Any]] else {return nil} for snap in snapDict { guard let user = User(dict: snap.value) else {continue} users.append(user) } self.users = users } } User Model: struct User { let name: String let username: String init?(dict: JSON) { guard let name = dict["name"] as? String, let email = dict["email"] as? String else {return nil} self.name = name self.username = email } } Firebase Snap: Snap (users) { 8CVeHMNHI6hZAWj1zhGHEjPwYYz1 = { email = "[email protected]"; name = Bshdjdj; }; 9CuqgR4Es7TOPPJQEpSnQXlfYnm1 = { email = "[email protected]"; name = "[email protected]"; }; DBqGWlpdJKME570euqUz2rqI5Z83 = { email = "[email protected]"; name = Test; }; } Fetch Function: func fetchUser() { let ref = Database.database().reference().child("users") ref.observe(.childAdded, with: { (snapshot) in let user = User(dict: snapshot.value as! JSON) self.users.append(user!) print(self.users) let new = HomeDataSource(with: snapshot) print(new) DispatchQueue.main.async(execute: { self.datasource = new self.collectionView?.reloadData() }) }, withCancel: nil) } Right now, I am getting an array of Users from Firebase, however my collection view won't update. My question is how should I update my Collection View Model and Fetching Function so it can fetch data from Firebase and Populate the Collection View Correctly? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
So the problem was the node I was targeting. Removed "child("users")" form fetching function and targeted the whole users node with uids. Then I was looping the snapshot.value while I casted it in [String:[String:Any]], because each snapshot.valuelooked like this (key: "vijwUkzAlbgcqjAammfy0JuVMB33", value: ["name": Silviu, "email": [email protected]]) Finally, I updated the HomeDataShource Class like this: class HomeDataSource: Datasource { var users: [User] init(with snapshot:DataSnapshot) { var users = [User]() let snapDict = snapshot.value as? [String:[String:Any]] for snap in snapDict! { guard let user = User(snap: snap.value) else {continue} users.append(user) } self.users = users }

Related questions

0 votes
    Details If you use the function createCustomToken to sign in the user and setting his custom claim, updating ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    what is the another name of weight update rule in adaline model based on its functionality? (a) LMS error learning ... (d) none of the mentioned Please answer the above question....
asked Sep 21, 2022 in Education by JackTerrance
0 votes
    What is synchronous update in hopfield model? (a) all units are updated simultaneously (b) a unit is selected at ... (d) none of the mentioned Please answer the above question....
asked Aug 30, 2022 in Education by JackTerrance
0 votes
    I have a model and store that take in values from my form fields. My form contains a textfield, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I am using Firebase authentication and react-redux in my app. I added firebase.auth().onAuthStateChanged to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am using Firebase authentication and react-redux in my app. I added firebase.auth().onAuthStateChanged to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am getting the error 'Uncaught TypeError: firebase.auth is not a function'. My code was working perfectly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    It is possible to use email verification with firebase but instead of sending a link they have to click, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have an application I'm building with Firebase. Essentially, I'm trying to build an authentication system ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Error: Type com.google.android.gms.common.internal.zzf is referenced as an interface from `com.google. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    My node 8 function, hosted at Firebase Cloud Functions isn't working as expected. I've a toLocaleString ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    My node 8 function, hosted at Firebase Cloud Functions isn't working as expected. I've a toLocaleString ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    My code: PhoneAuthProvider.getInstance().verifyPhoneNumber( phoneNumber, // Phone number to verify 60, // Timeout ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I replaced the Google Analytics SDK with the Firebase SDK. I'm looking to generate some of the previously- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    this is my first question here, i hope you can help me. I've been trying to get a specific path ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
...