简体   繁体   English

同步将图像上传到Firebase

[英]Upload image to Firebase Synchroniously

I am trying to upload a product to Database, and I want all information to be written in one transaction. 我正在尝试将产品上传到数据库,并且我希望所有信息都可以在一次交易中写入。 While this does happen, it doesn't for an uploaded image. 尽管确实发生了这种情况,但对于上传的图像却没有。 This is my code : 这是我的代码:

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)

My code for registering the product: 我注册产品的代码:

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)

}

I want the complete product to be registered before the segue is performed. 我希望在执行segue之前先注册完整的产品。 How can I do that ? 我怎样才能做到这一点 ?

As of right now, product is registered, segue is performed and product is loaded to CollectionView with default image, then product image is written to Firebase and then loaded to collectionView. 到目前为止,已经注册了产品,执行了segue并将产品以默认图像加载到CollectionView,然后将产品图像写入Firebase,然后加载到collectionView。 I want my product to load with the correct image from the start 我希望我的产品从一开始就加载正确的图像

The idea is to nest network calls , and in final one perfromSegue 这个想法是嵌套网络调用,最后是perfromSegue

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

              }
            }
     }
  }

You want the segue to happen when RegisterProductOnDatabase is finished with its asynchronous calls, so add a parameter to give it its own completion callback, and call it when all the asynchronous work is done: 您希望segue在RegisterProductOnDatabase以其异步调用完成时发生,因此添加一个参数以为其提供自己的完成回调,并在完成所有异步工作后调用它:

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

// all your code here

completionHandler()

}

Then call it like this: 然后这样称呼它:

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

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

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

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