简体   繁体   English

从Firebase的数据库下载帖子时出错(Swift 3)

[英]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. 我有一个社交应用,其中包含类似Facebook的帖子,但是当我尝试下载帖子时,它返回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 经过一些调试后,我将问题减少到此代码行,然后返回nil值

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 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 我假设currentUserdEXaVLDOSPfJa3zTyUNqAEtVuMR2 ,并且第一个获得以下查询的查询应返回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 ... 您在帖子中没有任何关于mt0fzirhMhazIcy90MRWuRpTfmE2引用,因此您永远都不会在第二个查询中得到任何返回的信息...

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: 该查询实际上正在下载所有帖子,但是它们被保存在一个非静态变量中(noob错误),所以我要做的唯一一件事就是在收到每个帖子之后重新加载数据,也就是说,它需要像这个:

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()
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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