0

I am trying to get data from firebase and use it as a model that I created.

Here is my model;

class UserData{
var nickname : String = ""
var onesignal_player_id : String = ""
var step_count : Int = 0
var total_point : Int = 0
var competitions : [String:Competition] = [String:Competition]()
}

class Competition{
    var end_date : String = ""
    var gift : String = ""
    var id: String = ""
    var name: String = ""
    var users : [String:Int] = [:]
}

and this is my function;

func getFirebaseData() {
    ref = Database.database().reference()
    ref.child("users").child("HXXNCXf6RRS4WVO12shZ3j15BnG3").observe(.value) { (snapshot) in
        if let snapshotValue = snapshot.value as? Dictionary<String,Any> {
            
            //change userData with the snapshotValue
            self.userData.nickname = snapshotValue["nickname"] as! String
            self.userData.step_count = snapshotValue["step_count"] as! Int
            self.userData.total_point = snapshotValue["total_point"] as! Int
            self.userData.onesignal_player_id = snapshotValue["onesignal_player_id"] as! String
            self.userData.competitions = snapshotValue["competitions"] as! [String:Competition]
            //reload UI with userData

            print(self.userData.competitions)
            
        } else {
            print("An error occured while assigning snapshotValue to userData")
        }
    }
}

This code gave me error like this;

Could not cast value of type '__NSDictionaryM' (0x1f47ada20) to 'StepCounterApp.Competition' (0x1004c06f0). 2021-01-02 23:05:49.985711+0300 StepCounterApp[32511:3685645] Could not cast value of type '__NSDictionaryM' (0x1f47ada20) to 'StepCounterApp.Competition' (0x1004c06f0).

but when i comment out the line included self.userData.competitions from getFirebaseData function, everything works fine.

What should I do? How can I use firebase data as a model?

Finally here is my firebase data;

Firebase data

  • It's not clear what it stored within *competitions* is that a Firebase Array e.g. competitions/0, competitions/1, competitions/2 etc? If so you may want to re-think that structure as array's are challenging to work with in the RTDB and there is probably a better structure. Also, if you ever want to update any child data in *users* you're going to want to include the node key in your Swift Models so you know what node the data belongs in. Also why are you storing competitions as `[String:Competition]`? String it not necessary, just an array of competitions would be better. – Jay Jan 03 '21 at 14:43

2 Answers2

0

The problem is in your data model. Declare your model data like this

class UserData {
    var nickname : String = ""
    var onesignal_player_id : String = ""
    var step_count : Int = 0
    var total_point : Int = 0
    var competitions : Competition = Competition()
}

class Competition{
    var end_date : String = ""
    var gift : String = ""
    var id: String = ""
    var name: String = ""
    var users : [String:Int] = [:]
    
    init() {
    }
    
    init(with dictionary: [String: Any]) {
        self.end_date = dictionary["end_date"] as! String
        self.gift = dictionary["gift"] as! String
        self.id = dictionary["id"] as! String
        self.name = dictionary["name"] as! String
        self.users = dictionary["users"] as! [String:Int]
    }
}

And inside the getFirebaseData funcation

self.userData.competitions = Competition(with: snapshotValue["competitions"] as! [String: Any])
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
0

The problem was in my data model and with the help of Raja Kishan's data model sugestion I fixed the problem.

First I changed the model little bit;

class UserData{
    var nickname : String = ""
    var onesignal_player_id : String = ""
    var step_count : Int = 0
    var total_point : Int = 0
    var competitions : [String:Competition] = [String:Competition]()
}

class Competition{
    var end_date : String = ""
    var gift : String = ""
    var id: Int = 0
    var name: String = ""
    var users : [String:Int] = [:]

    init() {
    }

    init(with dictionary: [String: Any]) {
        self.end_date = dictionary["end_date"] as! String
        self.gift = dictionary["gift"] as! String
        self.id = dictionary["id"] as! Int
        self.name = dictionary["name"] as! String
        self.users = dictionary["users"] as! [String:Int]
    }
}

Than I add a childSnapshot to my method so I can work directly the "competitions";

func getFirebaseData() {
    ref = Database.database().reference()
    ref.child("users").child("HXXNCXf6RRS4WVO12shZ3j15BnG3").observe(.value) { [self] (snapshot) in
        if let snapshotValue = snapshot.value as? [String:Any] {
            
            //change userData with the snapshotValue
            self.userData.nickname = snapshotValue["nickname"] as! String
            self.userData.step_count = snapshotValue["step_count"] as! Int
            self.userData.total_point = snapshotValue["total_point"] as! Int
            self.userData.onesignal_player_id = snapshotValue["onesignal_player_id"] as! String
            
            //******
            //This part of the coded added for to solve the problem starting from here 
            let childSnap = snapshot.childSnapshot(forPath: "competitions")
            if let childSnapValue = childSnap.value as? [String:Any] {
                childSnapValue.forEach { (element) in
                    self.userData.competitions.updateValue(Competition(with: element.value as! [String:Any]), forKey: element.key)
                }
            } else {
                print("something wrong with the childSnap")
            }
            //to here
            //******

        } else {
            print("An error occured while assigning snapshotValue to userData")
        }
    }
}