简体   繁体   English

SWIFT / iOS:出现屏幕时,数据需要花费几秒钟来加载

[英]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? 谁能验证这一点,并为我指出一个好的SWIFT教程或Stack Overflow解决方案?

UPDATE: I'm loading the data on the previous View Controller as suggested. 更新:我正在按照建议将数据加载到以前的View Controller上。 The issue that I'm running into is that the constants which I store my data are not accessible in prepareForSegue - 我遇到的问题是,在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: ! 不要覆盖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. 将加载代码移至您要调用的位置执行segue,然后将调用延迟到加载数据之后。 (IE move the perform segue call into the findObjectsInBackgroundWithBlock block after you get the items. (即在您获得商品后,将执行segue调用移至findObjectsInBackgroundWithBlock块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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