总结iOS开发中的断点续传与实践

2020-01-15 16:41:33丽君

另外由于采取子线程调用接口的方式 , 所以这边的 DownloadProgressBlock,success 和 failure Block 都需要回到主线程来处理。

断点续传实战

了解了原理和 AFHTTPRequestOperation 的例子之后 , 来看下实现断点续传的三种方式:

NSURLConnection

基于 NSURLConnection 实现断点续传 , 关键是满足 NSURLConnectionDataDelegate 协议,主要实现了如下三个方法:

清单 2. NSURLConnection 的实现


 // SWIFT 
 // 请求失败处理
 func connection(connection: NSURLConnection, 
 didFailWithError error: NSError) { 
  self.failureHandler(error: error) 
 } 

 // 接收到服务器响应是调用
 func connection(connection: NSURLConnection, 
 didReceiveResponse response: NSURLResponse) { 
  if self.totalLength != 0 { 
    return 
  } 

  self.writeHandle = NSFileHandle(forWritingAtPath: 
  FileManager.instance.cacheFilePath(self.fileName!)) 

  self.totalLength = response.expectedContentLength + self.currentLength 
 } 

 // 当服务器返回实体数据是调用
 func connection(connection: NSURLConnection, didReceiveData data: NSData) { 
  let length = data.length 

  // move to the end of file 
  self.writeHandle.seekToEndOfFile() 

  // write data to sanbox 
  self.writeHandle.writeData(data) 

  // calculate data length 
  self.currentLength = self.currentLength + length 

  print("currentLength(self.currentLength)-totalLength(self.totalLength)") 

  if (self.downloadProgressHandler != nil) { 
    self.downloadProgressHandler(bytes: length, totalBytes: 
    self.currentLength, totalBytesExpected: self.totalLength) 
  } 
 } 

 // 下载完毕后调用
 func connectionDidFinishLoading(connection: NSURLConnection) { 
  self.currentLength = 0 
  self.totalLength = 0 

  //close write handle 
  self.writeHandle.closeFile() 
  self.writeHandle = nil 

  let cacheFilePath = FileManager.instance.cacheFilePath(self.fileName!) 
  let documenFilePath = FileManager.instance.documentFilePath(self.fileName!) 

  do { 
    try FileManager.instance.moveItemAtPath(cacheFilePath, toPath: documenFilePath) 
  } catch let e as NSError { 
    print("Error occurred when to move file: (e)") 
  } 

  self.successHandler(responseObject:fileName!) 
 }

如图 5 所示 , 说明了 NSURLConnection 的一般处理流程。

图 5. NSURLConnection 流程

ios开发断点续传,ios断点续传,ios断点续传原理

根据图 5 的一般流程,在 didReceiveResponse 中初始化 fileHandler, 在 didReceiveData 中 , 将接收到的数据持久化的文件中 , 在 connectionDidFinishLoading 中,清空数据和关闭 fileHandler,并将文件保存到 Document 目录下。所以当请求出现异常或应用被用户杀掉,都可以通过持久化的中间文件来断点续传。初始化 NSURLConnection 的时候要注意设置 scheduleInRunLoop 为 NSRunLoopCommonModes,不然就会出现进度条 UI 无法更新的现象。