你也可以繼承AFURLConnectionOperation有2個新的屬性:(NSInteger)totalBytesSent
,並(NSInteger)totalBytesExpectedToSend
。你應該設置在NSURLConnection的回調這些屬性,像這樣:
- (void)connection:(NSURLConnection *)__unused connection
didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
[super connection: connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
self.totalBytesSent = totalBytesWritten;
self.totalBytesExpectedToSend = totalBytesExpectedToSend;
}
你的上傳進度塊可能是這樣的:
……(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSInteger queueTotalExpected = 0;
NSInteger queueTotalSent = 0;
for (AFURLConnectionOperation *operation in self.operationQueue) {
queueTotalExpected += operation.totalBytesExpectedToSend;
queueTotalSent += operation.totalBytesSent;
}
self.totalProgress = (double)queueTotalSent/(double)queueTotalExpected;
}];
這是不好的MVC編碼實踐設計模式。 –
也許吧。對於動態大小的內容而言,它是正確的工具。它將準確地顯示進度,而不必首先知道內容的大小。查找大文件(例如視頻或照片)的大小可能是一項昂貴的操作。 – clreina