从Firebase读取错误

时间:2018-02-16 17:53:30

标签: ios swift firebase

我使用以下代码将产品上传到Firebase:

let storageRef = Storage.storage().reference().child("ProductsImages").child(product.UniqueID()).child("MainImage.png")
            if let mainChosenImage = self.selectedImageToUpload
            {
                if let uploadData = UIImageJPEGRepresentation(mainChosenImage, 0.2)
                {
                    storageRef.putData(uploadData, metadata: nil)
                    {
                        (StorageMetaData, error) in
                        if error != nil
                        {
                            print(error)
                            return
                        }

                        self.mainImageURL = StorageMetaData?.downloadURL()?.absoluteString
                        if let urlString = self.mainImageURL
                        {
                            self.ref.child("Products").child(product.UniqueID()).child("MainImage").setValue(urlString)
                            self.ref.child("Users").child(user.uid).child("Products").child(product.UniqueID()).child("MainImage").setValue(urlString)
                            product.AddImageURLToProduct(URL: urlString)
                        }
                    }
                }
            }
            product.RegisterProductForAllUsers(database: self.ref)
            product.RegisterProductForAddingUser(database: self.ref)
            self.performSegue(withIdentifier: "unwindToMyProductsViewController", sender: self)

现在我知道写这样的图像是异步(1),但是在添加项目之后(假设我们现在忽略图片),我在Firebase中有这个:

saved Firebase Product

但是当我回到我的collectionView并加载信息时(它发生在ViewDidLoad方法中),这是我读到的信息:

Product information read

这是我的ViewDidLoad代码:

if let currentUserID = loggedOnUserID
    {
        // Retrieve the products and listen for changes
        databaseHandle = ref?.child("Users").child(currentUserID).child("Products").observe(.childAdded, with:
            { (snapshot) in

                // Code to execute when new product is added
                let prodValue = snapshot.value as? NSDictionary
                let prodName = prodValue?["Name"] as? String ?? ""
                let prodPrice = prodValue?["Price"] as? Double ?? -1
                let prodDesc = prodValue?["Description"] as? String ?? ""
                let prodURLS = prodValue?["MainImage"] as? String ?? ""
                let prodAmount = prodValue?["Amount"] as? Int ?? 0
                let prodID = snapshot.key

                let prodToAddToView = Product(name: prodName, price: prodPrice, currency: "NIS", description: prodDesc, location: "IL",
                                              toSell: false, toBuy: false, owner: currentUserID, uniqueID: prodID, amount: prodAmount)

                if (prodURLS != "")
                {
                    prodToAddToView.AddImageURLToProduct(URL: prodURLS)
                }

                self.products.append(prodToAddToView)
                DispatchQueue.main.async
                {
                    self.MyProductsCollection.reloadData()
                }
            }
        ) // Closes observe function

另外 - 我的代码写入数据库:

public func RegisterProductForAllUsers(database dataBase: DatabaseReference)
{
    dataBase.child("Products").child(self.UniqueID()).child("Name").setValue(self.Name())
    dataBase.child("Products").child(self.UniqueID()).child("UniqueID").setValue(self.UniqueID())
    dataBase.child("Products").child(self.UniqueID()).child("Price").setValue(self.Price())
    dataBase.child("Products").child(self.UniqueID()).child("Description").setValue(self.Description())
    dataBase.child("Products").child(self.UniqueID()).child("ToBuy?").setValue(self.m_ToBuy)
    dataBase.child("Products").child(self.UniqueID()).child("ToSell?").setValue(self.m_ToSell)
    dataBase.child("Products").child(self.UniqueID()).child("Owner").setValue(self.m_Owner)
    dataBase.child("Products").child(self.UniqueID()).child("Amount").setValue(self.m_Amount)
    dataBase.child("Products").child(self.UniqueID()).child("MainImage").setValue(self.m_PicturesURLs.first)
}

我先写“姓名”,这也许是我只能正确读名的原因?有没有办法让所有这些着作成为原子?

由于某种原因只有1个值。 (2) 1)任何使它同步的方法? 2)我如何阅读正确的值?

0 个答案:

没有答案