当时只显示一个图像来自数据库(Swift和Firebase)

时间:2017-11-09 03:35:52

标签: swift firebase firebase-realtime-database

首先,我是新手,所以请不要取笑我:)

基本上,我正在尝试展示产品的图像,但如果客户拒绝该产品,则该项目不会出现在他的帐户中。这就是我创建另一个表Rejected(setAcceptedOrRejected)的原因,我把产品的ID和客户端的Id放在一起,所以我不会看到他之前拒绝的项目。

我在这里尝试的是获取所有项目的列表(好)和拒绝项目的(坏)。然后比较它再次显示该项目的图片。

我的问题是我当时只想显示1张图片,如果客户端拒绝,那么它会显示下一张图片等等,但它不会再显示该图片。

我希望你能真正帮助我。

谢谢

func updateImage() {

    createListProductsBad ()

    var badnot = ""

    for bad2 in listProductsBad{
    badnot = bad2

    }


   Database.database().reference().child("Products").child(bad2)queryOrderedByKey().observe(.childAdded, with: { snapshot in

        let userInfo = snapshot.value as! NSDictionary

        let storageRef = Storage.storage().reference(forURL: profileUrl)
        storageRef.downloadURL(completion: { (url, error) in
            do {
                let data = try Data(contentsOf: url!)
                let image = UIImage(data: data as Data)
                self.productPhoto.image = image

            }
            catch _ {
                print("error")
            }




        })

    })


}

    func setAcceptedOrRejected() {

    let notThankyou  = [                  "ProductID": ProductId,
                                          "UserID": userUID
        ] as [String : Any]
    self.storyboard?.instantiateViewController(withIdentifier: "Home")
    self.refProducts.child("Rejected").childByAutoId().setValue(notThankyou)
}

    func createListProductsGood () {

    Database.database().reference().child("Products").queryOrderedByKey().observe(.childAdded, with: { snapshot in

        if !snapshot.exists() { return }



        let userInfo = snapshot.value as! NSDictionary

        let goodID = String(snapshot.key)


        for prod in self.listProductsBad{

            if (prod == goodID){
                print("Not good **********************")

            }else{

                if (goodID != "" ){
                    self.listProductsGood.append(prod)
                }


            }


         }
    })

}


   func createListProductsBad () {

Database.database().reference().child("Rejected").queryOrderedByKey().observe(.childAdded, with: { snapshot in

    let userInfo = snapshot.value as! NSDictionary
    let currentID = userInfo["UserID"] as! String
    let badProduct = userInfo["ProductID"] as! String

    if (self.userUID == currentID ){

       self.listProductsBad.append(badProduct)

    }
})

    }

}

1 个答案:

答案 0 :(得分:0)

 //These can also be swift's dictionaries, [String: AnyObject] or possibility arrays if done correctly. All depends on your style of programming - I prefer NSDictionaries just because.

let availableKeys: NSMutableDictionary = [:]
let rejectedKeys: NSMutableDictionary = [:]

//Might be a better way for you. Depends on what you are looking for.
func sortItems2() -> NSMutableDictionary{
    for rejKey in rejectedKeys.allKeys{
        //Removes if the rejected key is found in the available ones
        availableKeys.remove(rejKey)
    }
    return availableKeys
}