2

I'm developing an app at work, this is my first big application and in my smaller projects I didn't use caching at all.

What's currently happening

When the user logs on for their very first time they have to pull in all of the news article data, their website account information and a few other things. Once I have the data I cache it.

When the user navigates to a screen where the data is required, I attempt to fetch and parse it into a Model Class Object before I attempt to display. But as you may have guessed the fetching / parsing process is too slow and my TableViewController is empty.

What I've Tried

I've tried using a completion handler to fetch the data but the TableViewController is still empty on load. I feel like the data needs to be parsed and ready for the TableViewController. But I'm not sure where / what the best practice is for doing this.

Options I'm considering

I've thought about trying to parse immediately after I download it so that I'm fetching ready-made objects at run-time.

I've considered fetching all cashed data and parsing them when the application first loads and storing them all in AppDelegate at the class level (probably not the best idea).

I'm not really sure what my other options are, I guess that's why I'm here.

My Question

How should I handle the fetching of cached data in iOS?

2

If you have one set of data that you need to access in multiple places, I would recommend using a singleton.

Create a model for the data, download the data from the backend, parse it and store it in the singleton to access later on from where ever it needs to be. It makes no sense to waste all that time parsing multiple times.

The issue with the tableview is a separate issue. No matter what strategy you choose, accessing a lot of data will take time. If it takes too long you simply need to implement a loading screen of some sort. Even place a semi transparent UIView over your screen with an activity indicator spinning.

Once the data has been retrieved, dismiss the loading screen and reload the tableview. This is a standard approach to take. Pausing the UI or expecting all the data to have 0 access time are not reasonable assumptions / actions to take.

  • I'm glad I've actually done most of this. I've created a caching layer, when the app first loads I check if each cache has a value, if it does, the user is sent right the app, if a cache fails the check a flag is risen and the user is sent to the loading screen only to get what is needed and nothing more. Thanks simon – Dan Beaulieu Sep 23 '15 at 16:01
  • @simon can you please clarify how the use of a singleton would improve the performance here? – Howiecamp Jan 13 '17 at 3:32
  • @Howiecamp as I mentioned in my answer, the OP was discussing storing raw data, and then fetching / parsing for every screen. Obviously doing all of those things only once on app start up instead of on every screen, will have a noticeable performance difference. But the main reason for the suggestion wasn't performance, it was to reduce the complexity and adhere to standard approaches – Simon McLoughlin Jan 13 '17 at 8:37
  • @Simon - Understood. But why a singleton? That's what I'm not connecting the dots on. Thanks in advance. – Howiecamp Jan 13 '17 at 17:51
  • @Howiecamp why not a singleton? The OP suggested that the data will be required by different screens. Its very common to use a singleton in this case so you don't have to hang onto and pass around an instance when its not necessary to do so. Having to pass instances forward and backwards through flows is a very painful thing to do – Simon McLoughlin Jan 16 '17 at 8:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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