, SimpleURLConnections Apple. Apple NSURLConnection "connection: didReceiveData:"
" URL."
NSData , , , NSOutputStream, fileStream, . dataLength , NSURLConnection, "connection: didReceiveData:" , . :
@property (nonatomic, assign) long long dataWrittenForWholeConnection;
@property (nonatomic, assign) long long dataLengthOfWholeConnection;
: didReceiveResponse: NSURLResponse expectedContentLength .
self.dataWrittenForWholeConnection = 0;
self.dataLengthOfWholeConnection = [httpResponse expectedContentLength];
: didReceiveData: :
bytesWrittenSoFar += bytesWritten;
self.dataWrittenForWholeConnection += bytesWritten;
if (self.dataLengthOfWholeConnection != NSURLResponseUnknownLength) {
self.progressView.progress =
((float)self.dataWrittenForWholeConnection / (float)self.dataLengthOfWholeConnection);
}
:
assert(dataWrittenForWholeConnection == dataLengthOfWholeConnection);
SimpleURLConnections , , :
connection:didReceiveResponse:
Full size of data is 8789 bytes
connection:didReceiveData:
Data has 1408 bytes
Progress bar is at: 0.160200
1408 bytes of data were written, 1408 bytes so far
connection:didReceiveData:
Data has 1408 bytes
Progress bar is at: 0.320401
1408 bytes of data were written, 2816 bytes so far
connection:didReceiveData:
Data has 1408 bytes
Progress bar is at: 0.480601
1408 bytes of data were written, 4224 bytes so far
connection:didReceiveData:
Data has 1408 bytes
Progress bar is at: 0.640801
1408 bytes of data were written, 5632 bytes so far
connection:didReceiveData:
Data has 1408 bytes
Progress bar is at: 0.801001
1408 bytes of data were written, 7040 bytes so far
connection:didReceiveData:
Data has 1749 bytes
Progress bar is at: 1.000000
1749 bytes of data were written, 8789 bytes so far
connectionDidFinishLoading:
Connection Finished. 8789/8789 bytes of data were written.
, NSOutputStream "write: maxLength:" " ", - "connection: didReceiveData:" , , do/while . do/while.
, NSFileHandle , , : didReceiveData: call, . , , writeData: .
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
unsigned long long dataLength = [data length];
NSLog(@"Writing %qu bytes at %qu bytes", dataLength, [self.fileHandle offsetInFile]);
[self.fileHandle writeData:data];
NSLog(@"Wrote %qu bytes, now at %qu bytes", dataLength, [self.fileHandle offsetInFile]);
}