2015-10-13 95 views
0

我有60远程文件(影像)的列表,我想下载所有的人,但是当我尝试只是在这里下载15幅我的代码不完整的文件下载与alamofire

let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask) 

    for var i = 1; i < 61; i++ { 

     Alamofire.download(.GET, "https://domain.com/folder/\(i).jpg", destination: destination) 
      .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in 
       dispatch_async(dispatch_get_main_queue()) { 
        print("Total bytes read on main queue: \(totalBytesRead)") 
       } 
      } 
      .response { _, _, _, error in 
       if let error = error { 
        print("Failed with error: \(error)") 
       } else { 
        print("Downloaded file successfully") 
       } 
     } 

    } 

任何想法???

+0

说不上什么alamofire是,但它看起来像你正在下载太多太快,我会下载一个在同一时间,我就会把它的答案,这样的代码是更好 – Knight0fDragon

回答

0
func doDownload(i:int) 
{ 
    if(i > 60) 
    { 
     return; 
    } 
    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask) 

    Alamofire.download(.GET, "https://domain.com/folder/\(i).jpg", destination: destination) 
     .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in 
      dispatch_async(dispatch_get_main_queue()) { 
       print("Total bytes read on main queue: \(totalBytesRead)") 
      } 
     } 
     .response { _, _, _, error in 
      if let error = error { 
       print("Failed with error: \(error)") 
      } else { 
       print("Downloaded file successfully") 
       doDownload(i + 1); //If you only want to download on success do it here 
      } 
      //Otherwise doDownload(i + 1)here; 
    } 
} 
+0

@KnightOfDragon谢谢,我我会尝试一下并让你知道结果 –