简体   繁体   中英

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:

在此处输入图片说明

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:

在此处输入图片说明

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

        }
    }


}

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. (IE move the perform segue call into the findObjectsInBackgroundWithBlock block after you get the items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM