Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing, Displaying and Caching of large data

I'm trying to get data from some where and save it in a NSMutableArray of custom objects that contain that data and info.

Every time the user opens the app the same data is loaded from source and converted to my lovely array with custom objects. This takes a long time (~ 10-20s) and makes the app like a dumb. So what I tried to get the data and display it like in this link

What I tried:

  • save the array with custom objects to a file and load this file on next app start (if found) without a need to create the custom object from scratch again.
  • custom object encode & decode takes a long time at testing. Even if you save it in NSUserDefault.

Before any one start to answer this question plz. Have a look at the sample code and run it to make sure what I mean by retrieving the data and displaying it. in fast time after relaunch the sample and make what I wanna more clear :)

If anyone can make this sample more clear for me I will be thankful.

like image 458
Omarj Avatar asked Nov 02 '22 20:11

Omarj


1 Answers

In my opinion you only have two reasonable options: 1) the obvious is to use a database store and Core Data to manage your objects. 2) if you don't want to use Core Data, you might store each of your custom objects separately so that you can load them in batches.

If you want to go for the second option you can:

1) Implement the NSCoding protocol on your custom object. (it's just a bit of boilerplate code, very straightforward) 2) After you have loaded the data you can asynchronously save it to disk (this way you won't block the UI). 3) When the app launches, you might load a batch of your objects (e.g. 20, considering that you will only display ~10 at a given point). Then when the user scrolls through your content you can asynchronously load more of it in the same chunks so that they'll be ready to be displayed if/when needed.

This should greatly improve the performance of your app. If you use Core Data, instead, you can have it use a similar behaviour by setting the -[NSFetchRequest fetchBatchSize] property. This will take care of fetching data in batches automatically.

like image 170
Gianluca Tranchedone Avatar answered Nov 09 '22 10:11

Gianluca Tranchedone