Apple Apple - 2 months ago 12
Swift Question

How to Complete Async call before app loads?

I couldn't find the answer to this probably because I'm not really sure what I'm looking for since i just started programing a few weeks ago.

My storyboard entry point requires data that I get from an asynchronous session and JSON parse. Then once it gets the data it stores it to NSUserDefaults so it doesn't have to make the async call again and the app can access that data anytime.

I put my async call in the viewdidload of the storyboard entry point because as far as I know thats where the app starts. The issue is that the data isn't showing up until the app is started for a second time.

The data I'm getting from the async call only changes once every month so its not necessarily time sensitive.

How can I delay the app from getting to the storyboard entry point until the async call is finished?

Is that even the right way to go about it?

Should I switch to a synchronous call?

What if I changed the storyboard entry point to a view controller that looked like the app was loading and then when the async call finished, use a completion handler to perform segue to the view controller that requires the asynchronous call to finish?


Thanks Leo that worked.

Storing a variable when the app is first installed

I did two things here. First I created a new view controller that would execute the async call and segue to my main view controller when it finished. Then I detected if it was the first launch or not by using the above linked solution. Both of those together worked.

import Foundation
import UIKit

class FirstLoad: UIViewController {

var installedDate: NSDate? {
    get {
        return NSUserDefaults().objectForKey("installedDateKey") as? NSDate
    set {
        NSUserDefaults().setObject(newValue, forKey: "installedDateKey")

override func viewDidLoad() {

override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.

override func viewDidAppear(animated: Bool) {

func firstLoad() {
    if installedDate == nil {
        installedDate = NSDate()

        parseData(heroesDataProject) { heroesArrayFromParse in             //this function gets my json and the following code is executed after completion

            print("First Run")
            self.performSegueWithIdentifier("firstLoadToHomeMenu", sender: nil)

    } else {
        print("Not first run, installd on \(installedDate!)")
        performSegueWithIdentifier("firstLoadToHomeMenu", sender: nil)