Skip to content

Commit bdf8cea

Browse files
committed
完善了一个可能漏掉的错误
有可能写入流发生错误
1 parent dff1c05 commit bdf8cea

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

MJDownload/MJDownloadManager.m

+11-7
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,15 @@ - (void)didReceiveResponse:(NSHTTPURLResponse *)response
236236
- (void)didReceiveData:(NSData *)data
237237
{
238238
// 写数据
239-
[self.stream write:data.bytes maxLength:data.length];
240-
self.bytesWritten = data.length;
241-
242-
// 通知进度改变
243-
[self notifyProgressChange];
239+
NSInteger result = [self.stream write:data.bytes maxLength:data.length];
240+
241+
if (result == -1) {
242+
self.error = self.stream.streamError;
243+
[self.task cancel]; // 取消请求
244+
}else{
245+
self.bytesWritten = data.length;
246+
[self notifyProgressChange]; // 通知进度改变
247+
}
244248
}
245249

246250
- (void)didCompleteWithError:(NSError *)error
@@ -251,8 +255,8 @@ - (void)didCompleteWithError:(NSError *)error
251255
self.stream = nil;
252256
self.task = nil;
253257

254-
// 错误
255-
self.error = error;
258+
// 错误(避免nil的error覆盖掉之前设置的self.error)
259+
self.error = error ? error : self.error;
256260

257261
// 通知(如果下载完毕 或者 下载出错了)
258262
if (self.state == MJDownloadStateCompleted || error) {

0 commit comments

Comments
 (0)