gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:07:53 25 4
gpt4 key购买 nike

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

所以我是这样做的:

    ///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)

session 是这样设置的:

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),这很不错,但对于用户来说,查看下载计数器的时间很长。

而且计划是,拥有更多......所以我真的需要一种方法来更快地做到这一点。

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

提前致谢:)

最佳答案

尝试使用 json 和图像批处理进行优化(服务器端优化)。在一段时间内下载一个大文件总比下载很多小文件好。如果您始终需要所有这些,这对电池生命周期来说是一个巨大的胜利,正如 documentation 中所指出的那样。 .

关于iOS Swift 下载许多小文件的最快方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com