简体   繁体   中英

Error downloading posts from Firebase's Database (Swift 3)

I have a social app that have posts like Facebook but when i try to download the posts it returns nil.

FIRDatabase.database().reference().child("following").child(FIRAuth.auth()!.currentUser!.uid).queryOrderedByValue().queryEqual(toValue: true).observeSingleEvent(of: .value, with: {(snap) in
    if let snapDict = snap.value as? [String:AnyObject]{
        for each in snapDict{


            FIRDatabase.database().reference().child("Posts").child(String(each.key)).queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value, with: {(snapshot) in

                if (snapshot.value != nil)
                {
                    self.Posts.append(snapshot.value as! NSDictionary)


                }

            }){(error) in

                print(error.localizedDescription)
            }

        }

    }

    self.homeTableView.reloadData()

    self.aivLoading.stopAnimating()
})

after some debugging i reduce the problem to this line of code that return then nil value

FIRDatabase.database().reference().child("Posts").child(String(each.key)).queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value, with: {(snapshot) in

the other part of my code does return the list of people you follow and loops thought it.

this is the structure of my database.

{
  "Posts" : {
    "dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
      "-KbHUnL-RveUQa3MPSWp" : {
        "latitud" : "21.111401000574",
        "longitud" : "-89.6112191677094",
        "text" : "Fiesta en la Anahuac!!! ",
        "timestamp" : "1485295269.30773",
        "ubicacionN" : "Universidad Anáhuac Mayab"
      },
      "-KbI1azr6uFel-5uTZOD" : {
        "latitud" : "Optional(21.018988764483463)",
        "longitud" : "Optional(-89.614319546492695)",
        "text" : "Hola chicos",
        "timestamp" : "1485304393.77929",
        "ubicacionN" : "Calle 53-A 341"
      },
      "-KbNQWxjQhc0Ce_ZQbq9" : {
        "latitud" : "Optional(21.019219877217914)",
        "longitud" : "Optional(-89.614173537203683)",
        "text" : "Hola",
        "timestamp" : "1485394812.83039",
        "ubicacionN" : "Calle 53 341"
      }
    },
    "mt0fzirhMhazIcy90MRWuRpTfmE2" : {
      "-KbQOWfUnzY1JiS61J6-" : {
        "latitud" : "Optional(21.111502615883129)",
        "longitud" : "Optional(-89.611767497121221)",
        "text" : "Hola chicos!",
        "timestamp" : "1485444619.10931",
        "ubicacionN" : "Carretera Mérida-Progreso 96"
      }
    }
  },
  "follower" : {
    "dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
      "mt0fzirhMhazIcy90MRWuRpTfmE2" : true
    },
    "mt0fzirhMhazIcy90MRWuRpTfmE2" : {
      "dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : true
    }
  },
  "following" : {
    "dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
      "mt0fzirhMhazIcy90MRWuRpTfmE2" : true
    },
    "mt0fzirhMhazIcy90MRWuRpTfmE2" : {
      "dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : true
    }
  },
  "handles" : {
    "jcadmin" : "mt0fzirhMhazIcy90MRWuRpTfmE2",
    "jcbest" : "dEXaVLDOSPfJa3zTyUNqAEtVuMR2"
  },
  "user_profiles" : {
    "dEXaVLDOSPfJa3zTyUNqAEtVuMR2" : {
      "about" : "Hola Mundo",
      "handle" : "jcbest",
      "name" : "Juan Carlos Estevez Rodriguez",
      "profile_pic" : "https://firebasestorage.googleapis.com/v0/b/jalo-267da.appspot.com/o/user_profiles%2FOptional(%22dEXaVLDOSPfJa3zTyUNqAEtVuMR2%22)%2Fprofile_pic?alt=media&token=bfc3c516-7849-472c-b7cd-9668965a5dbe"
    },
    "mt0fzirhMhazIcy90MRWuRpTfmE2" : {
      "about" : "Hola chicos",
      "handle" : "jcadmin",
      "name" : "Juan Carlos",
      "profile_pic" : "https://firebasestorage.googleapis.com/v0/b/jalo-267da.appspot.com/o/user_profiles%2FOptional(%22mt0fzirhMhazIcy90MRWuRpTfmE2%22)%2Fprofile_pic?alt=media&token=b741b6c1-0bc5-446d-a1e5-159b21e770d2"
    }
  }
}

this is my entire code. https://www.dropbox.com/sh/u7saz4mdbehw1gd/AACv2rZH7M8jS_lU-plSqwc5a?dl=0

If this is a valid data set, then you're looking for the wrong thing in your query, but it could be that I just don't understand your data!

I'm assuming that the currentUser is dEXaVLDOSPfJa3zTyUNqAEtVuMR2 , and that the first query to get those following should return mt0fzirhMhazIcy90MRWuRpTfmE2

You don't have any reference to mt0fzirhMhazIcy90MRWuRpTfmE2 in the posts, so you're never going to get anything returned in the second query ...

I found the problem after a lot of debuging and as it tourns out the problem was that in this part

 FIRDatabase.database().reference().child("Posts").child(String(each.key)).queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value, with: {(snapshot) in

                if (snapshot.value != nil)
                {
                    self.Posts.append(snapshot.value as! NSDictionary)


                }

            }){(error) in

                print(error.localizedDescription)
            }

the Query was actually downloading all the posts but they were kept in a non static varaible (noob error) so the only thing I had to do was reload the data just after it got every post, that is to say that it needed to be like this:

FIRDatabase.database().reference().child("Jalas").child(each.key).queryOrderedByKey().observe(.childAdded, with: { (snapshot:FIRDataSnapshot) in

                        print(snapshot.value as! NSDictionary)

                        self.Jalas.append(snapshot.value as! NSDictionary)
                        self.homeTableView.reloadData()
                    })

so the complete code looks something like this:

        self.homeTableView.delegate = self
        self.homeTableView.dataSource = self

        self.loggedInUser = FIRAuth.auth()?.currentUser

        print("LoggedInUser: " + (self.loggedInUser?.uid)!)


        FIRDatabase.database().reference().child("following").child(FIRAuth.auth()!.currentUser!.uid).queryOrderedByValue().queryEqual(toValue: true).observeSingleEvent(of: .value, with: {(snap) in

            //if let snapDict = snap.value as? [String:AnyObject]{

            let sanpDict = snap.value as? [String:AnyObject]
            if (sanpDict != nil)
            {
                for each in sanpDict!{

                    print("each.key es: " + String(each.key))

                    FIRDatabase.database().reference().child("Jalas").child(each.key).queryOrderedByKey().observe(.childAdded, with: { (snapshot:FIRDataSnapshot) in

                        print(snapshot.value as! NSDictionary)

                        self.Jalas.append(snapshot.value as! NSDictionary)
                        self.homeTableView.reloadData()
                    })

                }
        }


            self.aivLoading.stopAnimating()
        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM