SB2015
SB2015

Reputation: 145

SWIFT / iOS: Data takes a few seconds to load when screen appears

I've created a user profile screen, and when loaded, the data takes a few seconds to appear. I'm looking for a more elegant solution.

Here's the view controller in the storyboard:

enter image description here

As you can see, there is placeholder text. The issue is that this text appears very briefly when this screen is loaded (before the data is retrieved from the database).

Here is what the screen looks like when the data is fully loaded:

enter image description here

I've seen that an Activity Indicator on the previous screen may be a good solution. Can anyone verify this and point me to a good SWIFT tutorial or Stack Overflow solution?

UPDATE: I'm loading the data on the previous View Controller as suggested. The issue that I'm running into is that the constants which I store my data are not accessible in prepareForSegue -

override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {


            let query = PFQuery(className:"UserProfileData")
            query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)

            query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in

                if error == nil {

                    if let objects = objects! as? [PFObject] {
                        for object in objects {

                            let yourselfObject = object["yourself"] as! String?
                            let brideGroomObject = object["brideGroom"] as! String?

                        }
                    }
                } else {
                    print(error)
                }
            }


}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "RSVPToUserProfile" {
        if let destination = segue.destinationViewController as? UserProfileViewController {

            destination.yourselfPassed = yourselfObject
            destination.brideGroomPassed = brideGroomObject

        }
    }


}

Upvotes: 0

Views: 2491

Answers (1)

Daniel T.
Daniel T.

Reputation: 33979

This one is actually very simple... Just remove the placeholder text from the storyboard. That way it won't "appear very briefly."

If you don't like the idea of a blank screen, then move the loading code to the place where the view is being presented. Don't present the view until after the loading code is complete. You will have to pass the data to the view controller at presentation time if you do this.

-- EDIT --

Do not override performSegueWithIdentifier:sender:! Doing so will not accomplish your goal.

I say again. Move the loading code to the place where you are calling perform segue, and delay the call until after your data is loaded. (I.E. move the perform segue call into the findObjectsInBackgroundWithBlock block after you get the items.

Upvotes: 3

Related Questions