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!

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

Austin Murtha
Austin Murtha
4,843 Points

Pause view while data loads Swift

Hi - I am enhancing the weather app from the Swift beginner course, and I am not able to find the solution to my problem. Currently the view loads with the stock weather, and then changes while the user is looking at the application. I think that is a poor experience.

I want to delay the page from loading until the app downloads the weather info and it is ready to display. I need about 1second. I am pretty sure this is done in appdelegate, but I can't find the code. Apologies for a noob question.

3 Answers

Patrick Cooney
Patrick Cooney
12,216 Points

So this is actually the perfect case for using NSUserDefaults. A lot of people abuse NSUserDefaults to store tons of info when they should be considering a lightweight database. If I were you I would store the most recently entered location in NSUserDefaults and then you can retrieve that NSUserDefaults value on launch and set the weather to it's value immediately. Here's a tutorial on NSUserDefaults.

Alternatively you can set a default "Getting Weather" or "Retrieving Weather" graphic that doesn't update until you have the local weather you're looking for. If I was going to do that, I would have that graphic and label be the default on launch. My method that retrieves the weather would compare the zip code entered by the user (or stored in NSUserDefaults) and the zip code in the JSON that your data object currently holds. If they match, the label and icon can then be updated. If they don't match it continues to show the "Retrieving" message until the data object has the data for the zip code you're expecting it to.

Austin Murtha
Austin Murtha
4,843 Points

Ahh That is actually a really good idea to set it to gathering weather.

Patrick Cooney
Patrick Cooney
12,216 Points

Here is one about pausing the launch screen. It sounds like that's what you were getting at. That post will tell you how to do it. That said, don't. Changing data is a better user experience than a launch screen that takes annoyingly long.

Austin Murtha
Austin Murtha
4,843 Points

Hi Patrick - would you just change the default screen? It looks weird when someone launches it and they see alcatraz instead of say New York.