Set the progress to UIProgressView when upload with NSURLConnection

前端 未结 1 884
星月不相逢
星月不相逢 2021-01-21 03:57

I\'m trying to refresh the progress bar in an UIProgressView for an upload request with NSURLConnection. The goal is to refresh the progress bar while uploading a p

相关标签:
1条回答
  • 2021-01-21 04:40

    You should really read a C tutorial on numerical types. Presumably both totalBytesWritten and totalBytesExpectedToWrite are an integer type, so dividing them will result in truncation - that is, the fractional part of the result will be gone. Unless the result is at 100%, the integral part is always 0, so all those divisions will result in zero. Try casting one or both of the variables to float or double to get sensible results.

    Also, UIProgressView doesn't accept values between 0 and 100 by default but between 0 and 1. All in all, you should write

    self.delegate.progressView.progress = ((float)totalBytesWritten / totalBytesExpectedToWrite);
    

    and it should work fine.

    Edit: the problem was that the data you were trying to upload was too small and it didn't need to be broken down to smaller chunks, so it was necessary to call this method only once. If you supply a great amount of data, then it will be able to be sent only in separate pieces, so the progress handler callback will be called multiple times.

    0 讨论(0)
提交回复
热议问题