J'écris une application iPad qui a besoin, pour télécharger de nombreux, mais assez petits fichiers .json et .jpg d'un serveur.iOS moyen le plus rapide pour télécharger de nombreux petits fichiers
donc prix que je fais comme ceci:
///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)
}
(je suis toujours en utilisant dataTaskWithURL)
Et voilà comment la session est configuré:
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)
}
Et oui cela fonctionne comme très bien. Je peux télécharger plus de 280 fichiers (.json et .jpg) en 22sec, ce qui est décent, mais très long pour un utilisateur, de regarder un compteur de téléchargement.
Et le plan est, d'en avoir plus que ça .. Donc j'ai vraiment besoin d'un moyen de le faire plus rapidement.
Je peux fournir plus de code que j'utilise, si nécessaire.
Merci à l'avance :)
Peut-être que la réponse du serveur est lente? BTW vous n'avez pas à mettre en œuvre la connexion manuellement et il existe des bibliothèques pour le faire en plusieurs lignes, vérifiez ceci: https://github.com/Alamofire/Alamofire – AaoIi
Je ne pense pas que le cas .. La connexion à la le serveur est assez rapide. – Bjqn
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/index.html#//apple_ref/occ/clm/NSData/dataWithContentsOfURL: La méthode dataWithContentsOfURL de NSData sera utile pour télécharger les petits fichiers –