iOS Swift最快的方式来下载许多小文件

问题描述:

我正在写一个iPad应用程序,需要从服务器下载许多但相当小的.json和.jpg文件。iOS Swift最快的方式来下载许多小文件

所以车费我这样做是这样的:

///Function to allow for recursive calls to syncronize inspections sequentially. 
    func getInspection(ip: String, view: sensorSyncronizationDelegate, idarr:[IdData], appDelegate: AppDelegate){ 
    let inspectionID = idarr[0] 
    var newArr = idarr 
    //A task is created for each inspection that needs to be downloaded, and the json is parsed and added to the database. 
    if self.session != nil { 
     let inspectionURL = NSURL(string: "http://\(ip)/inspections/\(inspectionID.id!).json") 
     let inspectionTask = self.session!.dataTaskWithURL(inspectionURL!) { (data, response, error) in 
      //If data is nil, end the task. 
      if data == nil { 
       view.setInspectionSyncCompleted() 
       view.completion("Error: Timeout please ensure Instrument is on, and attempt syncronization again") 
       print(error) 
       return 
      } 

      //if newArr is NOT empty make a recursiv call to getInspection() 
      newArr.removeAtIndex(0) 
      if !newArr.isEmpty{ 
       self.getInspection(ip, view: view, idarr: newArr, appDelegate: appDelegate) 
      }else{ 
       self.syncMisc(ip, view: view) 
      } 

(我一直在使用dataTaskWithURL)

这是会话如何设置:

var session : NSURLSession? 

///Function to set up various http configurations, and call the various syncronization functions. 
func syncWithSensor(view: sensorSyncronizationDelegate, ip: String!){ 

     //Session Configuration 
     let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
     config.allowsCellularAccess = true 
     config.timeoutIntervalForRequest = 30 
     config.timeoutIntervalForResource = 60 

     config.URLCache = nil 

     //Authentication config 
     let userpasswordString = "MAMA:PassWord" 
     let userpasswordData = userpasswordString.dataUsingEncoding(NSUTF8StringEncoding) 
     let base64encodedCreds = userpasswordData!.base64EncodedStringWithOptions([]) 
     let authString = "Basic \(base64encodedCreds)" 
     config.HTTPAdditionalHeaders = ["Authorization" : authString, "Connection" : "Upgrade"] 

     session = NSURLSession(configuration: config) 

    //Check if for some reason ip is invalid 
    if ip == nil{ 
     view.setSeriesSyncCompleted() 
     view.setTemplateSyncCompleted() 
     view.setInspectionSyncCompleted() 
     view.completion("Error: Failed to connect to ***, please reset connection") 
    } 

    //Call the inspection sync function. 
    syncInspections(ip, view: view) 
} 

//Function to respond to authentication challenges. 
func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) { 
    let credential = NSURLCredential(user: "MAMA", password: "PassWord", persistence: .ForSession) 
    completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential) 
} 

是的它工作得很好。我可以在22秒内下载280多个文件(.json和.jpg),这对于用户来说是相当不错的,但对于一个下载计数器来说很长时间。

而计划是,有更多的那..因此,我真的需要一种方法来更快地做到这一点。

如果需要,我可以提供更多我使用的代码。

感谢提前:)

+0

可能是来自服务器的响应速度慢?顺便说一句,你不必手动实现连接,并存在这样做在几行的图书馆,检查了这一点:https://github.com/Alamofire/Alamofire – AaoIi

+0

我不认为这种情况..连接到服务器速度不够.. – Bjqn

+0

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/index.html#//apple_ref/occ/clm/NSData/dataWithContentsOfURL: NSData的dataWithContentsOfURL方法将有助于下载小文件 –

尝试用JSON和图片批处理(服务器端优化)优化。在一段时间内下载一个大文件比许多小文件要好。如果你总是需要所有这些,那么这对电池寿命来说是一个巨大的胜利,因为它指向了documentation

+0

这是一些非常好的建议..但是这个下载操作,每天只能进行1-5次(在真实世界的情况下),但文件的数量可能会更大..我只想下载新的json文件不是所有的人(我有一个系统已经排序)? – Bjqn

+0

我将开始保存所有的JSON在一个大JSON或一个.zip文件.. – Bjqn