2

I've created a button when it is clicked, my app starts to download a json database which is stored in arrays.

When I start to download the database sometimes the internet is very slow and the device is not able to download the content, and the users can still use the app even though they didn't get the data they need.

What I want is a timer or a counter, which alerts the user when the request time exceeds 1 minute, and displays a message saying "No internet connection" or "Too slow to download the database".

I'd like you to give me at least an idea, because I don't even know what to search :(

thank you!

UPDATE: This is the code that I've got so far.

override func viewDidLoad() { super.viewDidLoad()

    let url = NSURL(string: "http://146.83.128.64/tongoy/g.php?&sala=-1&curso=-1&profesor=-1&semestre=-1&semestrec=-1&carrera=-1&area=-1")!

    let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in

        if let urlContent = data {

            do {

                let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)

                for json in jsonResult as! Array<AnyObject> {

                    if let area = json["area"] as? String {
                        self.arregloArea.append(area)
                    }
                    if let id = json["id"] as? Int {
                        self.arregloID.append(id)
                    }
                    if let curso = json["curso"] as? String {
                        self.arregloCursos.append(curso)
                    }

                    if let carreras = json["carreras"] as? Array<AnyObject> {
                        for item in carreras {
                            if let id = item["id"] as? Int {
                                print(id)
                            }
                            if let nombre = item["nombre"] as? String {
                                print(nombre)
                            }
                            if let semestre = item["semestre"] as? Int {
                                print(semestre)
                            }
                            if let curso = item["curso"] as? Int {
                                print(curso)
                            }
                        }
                    }
                }
            } catch {
                print("JSON serialization failed")
            }

            dump(arregloCursos)
            dump(arregloArea)
            dump(arregloID)

        }
    }
    task.resume()
}
papuehl
  • 53
  • 7
  • How are you downloading the data? There is probably a timeout property you can use. – Daniel Storm Aug 27 '16 at 18:04
  • `NSURLSession` is widely customizable. – vadian Aug 27 '16 at 18:13
  • Show your code. How are you downloading your data? You should use URLSession. You can also make sure the device is connected to the wifi network using Reachability – Leo Dabus Aug 27 '16 at 18:13
  • You can use the `timeOut ` property of NSURLSession. Check this question . http://stackoverflow.com/questions/23428793/nsurlsession-how-to-increase-time-out-for-url-requests/30427187#30427187 – Anil Varghese Aug 27 '16 at 18:14
  • I don't really have a code to do that yet, I download the database with `NSJSONSerialization` using the link of my database – papuehl Aug 27 '16 at 19:18
  • I've updated my question with the code that I've got so far :) thank for the feedback :D – papuehl Aug 27 '16 at 19:33

1 Answers1

0
let request = NSMutableURLRequest(URL: url!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)

This will take the URL of your JSON database, set as a variable url, and then request it with a timeout interval of 5.0 seconds, which you can adjust to fit your needs! Good luck

UPDATE:

let url = NSURL(string: "http://146.83.128.64/tongoy/g.php?&sala=-1&curso=-1&profesor=-1&semestre=-1&semestrec=-1&carrera=-1&area=-1")!

let request = NSMutableURLRequest(URL: url)
let taskConfiguration =     NSURLSessionConfiguration.defaultSessionConfiguration()
taskConfiguration.timeoutIntervalForRequest = 10
taskConfiguration.timeoutIntervalForResource = 10
self.session = NSURLSession(configuration: taskConfiguration, delegate: self.delegates, delegateQueue: nil)


let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        let json:JSON = JSON(data: data!)
        onCompletion(json, error)
    })

NOTE:

Now that you have a timeout, you will have to handle if you don't receive data back / timeout. So check if your data == nil, or whatever your preference is, and do something, to avoid a crash.

SUMMARY:

Essentially you are creating your own NSURLSessionConfiguration, then starting your session based on the configurations you applied.

Jake Braden
  • 489
  • 5
  • 15