1

XCode: 11, Swift: 5, iOS: 13

My ProgressHUD (showSuccess and showError, e.g.) only appears if I use a function a second time. After restarting the app and for example, uploading a picture, no progress image appears. When I upload a picture again, it appears. I have this behaviour for all of my progressHUD elements. Why is this happening?

Example code:

func uploadDataToDatabase(imageUrl: String) {
    let databaseRef = Database.database().reference().child("posts")
    let newPostId = databaseRef.childByAutoId().key

    let newPostRefernce = databaseRef.child(newPostId)

    let dic = ["imageUrl" : imageUrl, "postText" : postTextView.text] as [String : Any]
    newPostRefernce.setValue(dic) { (error, ref) in
        if error != nil {
            ProgressHUD.showError("Fehler, Daten konnten nicht hochgeladen werden")
            return
        }
        ProgressHUD.showSuccess("Post erstellt")
        self.remove()
        self.handleShareAndAbortButton()
        self.tabBarController?.selectedIndex = 0
    }
}
JohnDole
  • 525
  • 5
  • 16

2 Answers2

0

Try Running in main thread

DispatchQueue.main.async(execute: {

                        ProgressHUD.showError("Fehler, Daten konnten nicht hochgeladen werden")

                    })
Prashanth Thota
  • 157
  • 1
  • 11
  • No changes. Progress Icon only appears after a second time (or more) - but not for the first time, I upload a picture – JohnDole Nov 22 '19 at 11:43
0

Try to construct a completion block for your function so it is only executed when uploading to database is completed (or not). Use ProgressHUD functions inside completion block when calling the function.