CSMariah CSMariah - 2 months ago
164 0

I'm trying to retrieve user data from dashboard to a table view based on a specific node! (Category sub-node in Devices Node)

There's a table view called home that contains 6 categories whenever the user clicks on any category another table view should be viewed that contains the image, DeviceName (From Devices Node) and name and city (From UserProfile Node) in each cell!

"And whenever the user clicks on any cell there should be another viewController that contains all of the user info in UserProfile node and all of the Device info (one of his/her Devices) from Devices Node!


I followed a method I saw in one of stackoverflow questions! Using mutable array!

But this method was only for retrieving data from on basic node!

So based on what I want the data will be retrieved from two nodes from the firebase!

*Can I still use this method? If yes, is what I have done, correct? It did not retrieve anything actually on the table view!

*This way was for retrieving only texts! Now I want to retrieve images as well! So Can I still using this method!

I'm trying to find a simple way to do that!

Can you help me? And guide me to the right direction?

I'm looking for your answer, thanks!

Swift

Retrieving Data from Firebase to TableView (Swift Project)

Here's the dashboard structure:
    
      {
    "Devices" : {
    "1EJ8QmEQBJfBez7PMADbftCjVff1" : {
      "Device1" : {
        "Category" : "Others",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : ""
      },
      "Device2" : {
        "Category" : "Chairs",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : ""
      },
      "Device3" : {
        "Category" : "Others",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : ""
      }
    },
    "97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
      "Device1" : {
        "Category" : "Others",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : ""
      },
      "Device2" : {
        "Category" : "Books",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : ""
      },
      "Device3" : {
        "Category" : "Chairs",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : ""
        }
       }
      },
     "UserProfile" : {
    "1EJ8QmEQBJfBez7PMADbftCjVff1" : {
      "city" : "",
      "email" : "",
      "name" : "",
      "phone" : ""
    },
    "97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
      "city" : "",
      "email" : "",
      "name" : "",
      "phone" : ""
       },
     }
    }



Here's the rule I have done: 

        {
      "rules": {
      ".read": "true",
      ".write": "true",
      "Devices": {
      "$Devices_id": {
      ".indexOn": "Category"
      }
       }, "UserProfile": {
      "$User_id": {
       ".indexOn": "Category"
       }
     }   
    }
    }



And here's what I have done in the code so far: 
        
        class CategoryDeviceViewController: UITableViewController{

    
     var DeviceNamesArray: NSMutableArray = []
     var ProfileNames: NSMutableArray = []
     var titlestring: String!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
         self.navigationItem.title = titlestring

        let ref = FIRDatabase.database().reference().child("Devices")

        ref.queryOrderedByChild("Category").queryEqualToValue(titlestring)
            .observeEventType(.Value, withBlock: { snapshot in
                
                if let dict = snapshot.value as? NSMutableDictionary{
                   
                    
                    for (key,value) in dict {
                        let mainDict = NSMutableDictionary()
                        mainDict.setObject(key, forKey: "userid")
                        
                        
                        if let dictnew = value as? NSMutableDictionary {
                            
                            if let metname = dictnew["DeviceName"] as? String
                            {
                                mainDict.setObject(metname, forKey: "DeviceName")
                            }
                            if let metname = dictnew["Description"] as? String
                            {
                                mainDict.setObject(metname, forKey: "Description")
                            }
                            if let metname = dictnew["Category"] as? String
                            {
                                mainDict.setObject(metname, forKey: "Category")
                            }
                            if let metname = dictnew["ImageUrl"] as? String
                            {
                                mainDict.setObject(metname, forKey: "ImageUrl")
                            }
                        }
                
                        self.DeviceNamesArray.addObject(mainDict)
                      
                    }
                  
                }
                
                
                let refer = FIRDatabase.database().reference().child("UserProfile")
                
                refer.queryOrderedByChild("Category").observeEventType(.Value, withBlock: { snapshot in
                    
                    if let dict = snapshot.value as? NSMutableDictionary{
                        
                        
                        for (key,value) in dict {
                            let mainDict = NSMutableDictionary()
                            mainDict.setObject(key, forKey: "userid")
                            
                            
                            if let dictnew = value as? NSMutableDictionary {
                                
                                if let metname = dictnew["name"] as? String
                                {
                                    mainDict.setObject(metname, forKey: "name")
                                }
                                if let metname = dictnew["city"] as? String
                                {
                                    mainDict.setObject(metname, forKey: "city")
                                }
                                if let metname = dictnew["phone"] as? String
                                {
                                    mainDict.setObject(metname, forKey: "phone")
                                }
                            }
                            
                            self.ProfileNames.addObject(mainDict)
                            
                        }
                        
                    }
                    
                    
                    
                })
                
              self.tableView.reloadData()
                
            })
        

  

        // self.tableView.reloadData()
    }

    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return DeviceNamesArray.count
       
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CategotyDeviceCellViewCell

        
        if let name = DeviceNamesArray[indexPath.row] as? NSMutableDictionary {
        cell.configureCellone((name["DeviceName"] as? String)!)
        }
        if let provider = ProfileNames[indexPath.row]as? NSMutableDictionary {
        cell.configureCellTwo((provider["name"] as? String)!)
        }
        
        return cell
    }