Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Delay in getting data from NSDictionary

override func viewDidLoad() {
        super.viewDidLoad()

        let baseURL = NSURL(string: "http://numbersapi.com/\(number)/")

    }

func triviaFunc(){
        let baseURL = NSURL(string: "http://numbersapi.com/\(number)/")
        let triviaURL = NSURL(string: "trivia?json", relativeToURL: baseURL)

        let downloadTaskTrivia: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(triviaURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

            if(error == nil){
                let dataObject = NSData(contentsOfURL: location)
                let triviaDictionary: NSMutableDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSMutableDictionary
                let trivia = Trivia(triviaDictionary: triviaDictionary)

                self.triviaLabel.text = trivia.text

            }


        })
        downloadTaskTrivia.resume()
    }

@IBAction func showNumbersFacts() {
        var color = colorWheel.randomColor()
        view.backgroundColor = color
        triviaFunc()

        //numbersButtonText.tintColor = color
    }


Data.swift:

struct Trivia {
    var text: String
    var number: Int

    init(triviaDictionary: NSMutableDictionary){
        text = triviaDictionary["text"] as String
        number = triviaDictionary["number"] as Int
    }
}

Everytime I hit the button, I want to show text from Dictionary (now NSMutableDictionary - it doesn't matter). It is happening, but maybe after 5 or 6 seconds.

Second question. Is this solution same for getting token from API (if user is signing in)? Is NSDictionary permanent storage? What if I want to update data? For example, I have five todos, I will add one on a server and want to update them in iOS app. So what now?

Nobody?