将图像同步上传到Firebase

时间:2018-02-17 21:49:11

标签: ios swift 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
                        {
                            // MARK - 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.RegisterProductOnDatabase(database: self.ref)
            self.performSegue(withIdentifier: "unwindToMyProductsViewController", sender: self)

我注册产品的代码:

public func RegisterProductOnDatabase(database dataBase: DatabaseReference)
{
    // Run in one transaction

    let key = dataBase.child("Products").child(self.UniqueID()).key
    let thisProductToRegister : [String:Any] = [
                            "Name": self.Name(),
                            "UniqueID": self.UniqueID(),
                            "Price": self.Price(),
                            "Description": self.Description(),
                            "ToBuy?": self.IsToBuy(),
                            "ToSell?": self.IsToSell(),
                            "Owner": self.m_Owner,
                            "Amount": self.Amount(),
                            "MainImage": self.m_PicturesURLs.first
                          ]

    let childUpdates = ["/Products/\(key)/": thisProductToRegister,
                        "/Users/\(self.m_Owner)/Products/\(key)/": thisProductToRegister]

    dataBase.updateChildValues(childUpdates)

}

我希望在执行segue之前注册完整的产品。我怎样才能做到这一点 ?

截至目前,产品已注册,已执行segue并使用默认图像将产品加载到CollectionView,然后将产品图像写入Firebase,然后加载到collectionView。我希望我的产品从头开始加载正确的图像

2 个答案:

答案 0 :(得分:0)

想法是嵌套网络呼叫,最后一个是perfromSegue

 CallAPI1
  {
     if(sucess)
     {  
            CallAPI2
            {
              if(sucess)
              {  
                  self.performSegue(withIdentifier: "unwindToMyProductsViewController", sender: self)

              }
            }
     }
  }

答案 1 :(得分:0)

当RegisterProductOnDatabase完成异步调用时,你希望segue发生,所以添加一个参数给它自己的完成回调,并在完成所有异步工作后调用它:

public func RegisterProductOnDatabase(database dataBase: DatabaseReference, completionHandler: @escaping () -> Void) {

// all your code here

completionHandler()

}

然后这样称呼:

product.RegisterProductOnDatabase(database: self.ref, completionHandler: {

self.performSegue(withIdentifier: "unwindToMyProductsViewController", sender: self)
})