How can I cache Javascript and JSON data in my iPhone application?

I am developing my own iPhone application in Titanium. In this application, I use data from a remote API (which I developed in Rails 3). I want the user to cache the API data on their phones as much as possible. I need help - this is the concept of caching. What is the best way to do this? The nature of the data in the API is that it must be relevant. Because it is contact information that can change at any time.

I do not know how the caching process will work. If someone can explain the best way to manage the caching process for the API, I would be more than happy!

I am using JSON and Javascript.

+4
source share
4 answers

β€œThe nature of the data in the API is that it must be relevant. Because it’s contact information that can change at any time.”

If true, this makes caching unnecessary, as you need to compare the cache file with the data to check for changes, thereby making the cache pointless.

The only reason you can still cache data is to make it available offline. In this case, I would use the SQLite database, which is native to iphone.

+1
source

titanium-cache is clean code with unit tests and provides sample code in readme. I combined this with my own project in minutes, and it worked well.

+1
source

I think the type of cache is application dependent.

You can cache data by:

  • customer;
  • server;
  • another network element.

The critical point is updating data. A bad algorithm creates inconsistent data.

You can find interesting information on the literature of distributed systems.

Bye

0
source

A couple of options here.

1) You can use ASIHTTPRequest and ignore cache headers to cache everything. When your application is in use, you can determine if the cache hits. If it hits, you delete the request to the server after the cache has hit to request any new data. You can do this by adding a random URL at the end of the URL, as cache keys are disconnected from the URL. If you have a good connection and new data, download it. Otherwise, do nothing, and your user has the latest data when using the application under a good connection.

2) Make the most of # 1, always clicking on the cache, but instead of launching a non-cached version of the same request to the server after getting into the cache, disable the check for lack of caching to check if the data is updated, If it were so, handle the full API request without caching. If this is not the case or failed, you can do nothing.

0
source

Source: https://habr.com/ru/post/1346369/


All Articles