3

I think I have got a little performance problem with my iPhone app.

I like to show you how I load Data from the Internet, store them on the Phone and reload them to print it out on a table view. I have several modules on the app so I hope it is ok, if I explain the general way I use to do and maybe one of you got an idea.

The amount of data is about 250 datasets with 10-20 data fields (integers / text).

Step 1: Getting a JSON data string by URL-request

I’ll start a request to an MySQL-Database by an URL-Request with some restrict values (for example city_id and last_update_time) to limit the result.

Step 2: Saving the JSON-Objects to a SQLite3 database on the iPhone.

I’ll do this because I like to use the data also in offline mode. This part should be the heavies’ one, because it takes 3/4 of the total time. I run through the objects and check if I saved this object once before. (Here I use an import_id to identify the MySQL-Database-Objects) If I did found it, I will update this dataset otherwise, I'll insert it.

Step 3: Reload the Data from the SQLite and put it into an NSMutableArray

This Array will be used by the TableView.

Note: The Data will be load in the main thread. Other threads will be used to lazy load some thumbs or showing a spinning wheel.

Is there a big mistake in the procedure? Should I load the data in another thread, but what should I display at this time in the view?

What is the best way?

  • Your thinking seems fine; however, I would load and populate the db in the background and maybe show an activity indicator to the user. – Jeremy Jun 26 '13 at 15:25
  • Basically fine, though it may be wise to figure out how to use a "lazy" array scheme that only loads individual elements from the DB when referenced (kinda similar to how TableViews manage rows). There are several ways to handle this. (Note that loading the entire array during startup produces a "bad user experience" and may cause enough delay to cause the app to be killed.) – Hot Licks Jun 26 '13 at 15:32
  • @Jeremy I implemented a "Spinning Wheel" to show the user, that data will be loaded – Dennis Peters Jun 27 '13 at 09:32
  • @HotLicks Maybe i will try to show only the first rows to the user, load the sata from Step2 in a background process and update the table view after this. Maybe i show the user more rows if he scrolls down or clicks to "load more data" .. i think the first variant got a better user experience. – Dennis Peters Jun 27 '13 at 09:37

2 Answers2

4

Here are some ideas:

  1. Use AFNetworking to make life easier for yourself. It makes getting JSON returns from web services asynchronous and easy.
  2. Rather than use SQLite directly use Core Data. It's so much easier to use this for any real persistence in your application than anything else.
  3. Rather than doing it manually this way, with your results in Core Data you (see step 2) you can use NSFetchedResultsController to load your table view from Core Data directly. This takes care of a lot of stuff for you, such as responding to changes, efficient memory management, handling changes in the data, etc.
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Thanks for this nice ideas and i think i will use them in a future project, but at the moment i hope i could fix the problem without changing such basicly things like the use of SQLite or Core Data. AFNetworking looks nice too, but Step1 (Load JSON Data) don't takes so much time. I think i have too put the complete process of loading and saving data (Step 1 + 2) into a background process. I hope this solves the performance problem. – Dennis Peters Jun 27 '13 at 09:47
2
  • Embed a json file into your app bundle that involves the current data from your json parse. This will be used for the first time offline using just for the case,
  • Update your json file whenever the device is online, but only update if your parse version is different than you have.
  • Use that json file anytime your device is offline
  • Since your json file in your bundle will be the newest version all the time, load your table data out of it.

Hope these will help you.

Engnyl
  • 1,380
  • 16
  • 24