I am writing an iOS application in XCode. The data loaded for the multitudes of viewcontrollers is generally loaded from the DB upon the viewDidLoad() or viewDidAppear() for that particular VC, or it it is passed from one VC to another upon IBAction button clicks.

I am wondering if there is a better/more efficient way to do this?

Should I load all data at the launch of the application? In my opinion this would not be a good idea considering that would require many different threads pulling from different parts of the DB all at the same time (or synchronously chained) at the onset. I am concerned this would slow the app's launch considerably.

Additionally, I think to update data, the viewDidLoad() and viewDidAppear() pulls couldn't be wholly removed anyway. In fact, I do not really see any particular benefit in doing a full load at the application launch, but I wanted to be doubly sure that I am correct in this assessment and am not missing anything.

Is there any benefit to doing the load this way? Is there any downside to doing it the way I have it set up already?

  • You haven't defined better/more efficient in your specific context. Do you want the app to load faster or to run more quickly once it is loaded (those two things might be mutually exclusive)? – Robert Harvey Nov 9 '17 at 17:23
  • @RobertHarvey I mean in terms of loading faster – Alek Piasecki Nov 10 '17 at 18:34

One approach that I've used and seen used for this kind of thing is that you can load the application and start background threads that start pre-fectching data that you know you will need. Typically, you load the data for the initial view before displaying it. For example, lets say you have a main page/window that has a 'messages' menu. On that messages menu, you want to show how many new messages there are. You retrieve the ids of the new messages and display the count. At the same time you start retrieving the data for each of the new messages.

This can produce a really nice user experience because often by the time the user selects to view the messages, you are done loading them. Even if you haven't finished, you have a head start which reduces the wait time.

The downside is that this is more difficult to design and implement because it requires thread coordination and your UI needs to deal with the situation that the user has selected something that is not yet loaded. One trick is to build a data layer that hides this complexity and appears just like a lazy load to the outside. You call for data and it blocks until it's available. You can also use this layer to re-prioritize the fetching based on what the user is trying to do.

  • Does XCode have something similar to async in C#? – Robert Harvey Nov 9 '17 at 17:53
  • @RobertHarvey I really have no idea. Threads were mentioned so I assume there's some way to make use of them. – JimmyJames Nov 9 '17 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.