LEVIS OGCPAS
LEVIS OGCPAS

Reputation: 229

Firebase - Array of NSObjects not returning normal count value

I'm trying to query the users under specific company, Then I append it to an NSObjects Array.

Code:

var userDetails = [UserDetails]()
func getDriversInfoWithCompany() {

      FIRDatabase.database().reference().child(“users").queryOrderedByChild("company").queryEqualToValue(“KFC").observeEventType(.ChildAdded, withBlock: { (snapshot) in

        let details = UserDetails(key: snapshot.key, snapshot: snapshot.value  as! Dictionary<String, AnyObject>)

        self.userDetails.append(details)

    self.loadStaffs()                

     }, withCancelBlock: nil)

}

func loadStaffs() {

    let count = self.userDetails.count

    print(count)

    for i in 0...count {

        print(self.userDetails[i].username)

    } 

}

And I'm using a for loop to get it's usernames. But I'm having an error fatal error: Index out of range Then I print() the count and the output is 1 2 3 4 Instead of just 4. I think How can I get the final value 4 so if i loop it it will not give me an error. Thanks!

EDIT:

Code:
    Snap (NU4chDtG7eWKXgPaTuFFDfWXWcf2) {
    company = "KFC";
    email = "[email protected]";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/NU4chDtG7eWKXgPaTuFFDfWXWcf2%2F492003486?alt=media&token=34cecf1e-d65f-4856-8aac-3afdcf9d451e";
    userRole = DISPATCH;
    username = UserName;
    }
    Snap (blkSE7N8GEPeoObNCWLhjyUnkSu2) {
    company = "KFC";
    email = "[email protected]";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FblkSE7N8GEPeoObNCWLhjyUnkSu2?alt=media&token=b97fd727-12f8-4725-a830-67133de78aab";
    userRole = SHIPPER;
    username = UserName;
    }
    Snap (d605A6Mc0hRWygvK9fjwUnPfliU2) {
    company = "KFC";
    email = "[email protected]";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2Fd605A6Mc0hRWygvK9fjwUnPfliU2?alt=media&token=f35347ca-8da5-402d-a6b6-c3a56f59cbcf";
    userRole = DRIVER;
    username = UserName;
    }
    Snap (fGfIKyF8g5WAOLBdc5dpNSWmaGy2) {
    company = "KFC";
    email = "[email protected]";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FfGfIKyF8g5WAOLBdc5dpNSWmaGy2?alt=media&token=79735c4e-aa70-4cb0-8a16-b6efedb668a2";
    userRole = DISPATCHER;
    username = "UserName";
    }

Code:

Snap (users) {

NU4chDtG7eWKXgPaTuFFDfWXWcf2 =     {

company = "KFC";

email = "[email protected]";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/NU4chDtG7eWKXgPaTuFFDfWXWcf2%2F492003486?alt=media&token=34cecf1e-d65f-4856-8aac-3afdcf9d451e";

userRole = DISPATCH;

username = UserName;

};

blkSE7N8GEPeoObNCWLhjyUnkSu2 =     {

company = "KFC";

email = "[email protected]";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FblkSE7N8GEPeoObNCWLhjyUnkSu2?alt=media&token=b97fd727-12f8-4725-a830-67133de78aab";

userRole = SHIPPER;

username = UserName;

};

d605A6Mc0hRWygvK9fjwUnPfliU2 =     {

company = "KFC";

email = "[email protected]";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2Fd605A6Mc0hRWygvK9fjwUnPfliU2?alt=media&token=f35347ca-8da5-402d-a6b6-c3a56f59cbcf";

userRole = DRIVER;

username = UserName;

};

fGfIKyF8g5WAOLBdc5dpNSWmaGy2 =     {

company = "KFC";

email = "[email protected]";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FfGfIKyF8g5WAOLBdc5dpNSWmaGy2?alt=media&token=79735c4e-aa70-4cb0-8a16-b6efedb668a2";

userRole = DISPATCHER;

username = "UserName";

};

}

The first one is .ChildValue, And the second one is .Value. Thanks!

Upvotes: 0

Views: 39

Answers (1)

Dravidian
Dravidian

Reputation: 9945

Try changing your loadStaff() function to :-

func loadStaffs() {

        let count = self.userDetails.count
        print(count)

            for i in 0...count-1 {

             print(self.userDetails[i].username)

             } 
         }

For retrieving use :-

func getDriversInfoWithCompany() {

  FIRDatabase.database().reference().child(“users").queryOrderedByChild("company").queryEqualToValue(“KFC").observeEventType(.Value, withBlock: { (snapshot) in
    if let snapDict = snapshot.value as? [String:AnyObject]{ 

      for each in snapDict as [String:AnyObject]{

      let details = UserDetails(key: each.0, snapshot: each.1  as! Dictionary<String, AnyObject>)

     self.userDetails.append(details)

     self.loadStaffs()                

   } 
  }
 }, withCancelBlock: nil)

}

Upvotes: 1

Related Questions