-2

I am certainly a newbie to android.

I am building a News app. I want to add a caching feature in the app like that in chrome for android(offlin mode) and Instagram.

The data for the app will go via json from the server. So what I want is, the app requests the data from server to load the feed, and the data gets cached so that if next time the app is opened without internet connection, the cached posts get displayed(the number of posts would be limited) And the main thing, the complete articles(by clicking the post in feed and reading the whole article in another activity) that users views should also get cached such that there would be a maximum of "n" number of posts in the cache at any time, if cache get filled with n posts, the last post added to the cache would get removed and so on...

How to do I achieve this caching? As with Picasso, the cache gets reset every time the app is killed. And sharedPreferences and SQLite are kind of permanent storages.

So what is the best way to achieve this kind of cache. Are there any libraries that can be used, or should I manage to do it with sharedPreferences/SQLite/Storage??

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

0

Depending on the size of the data you want to store, presumably news feed history is going to be a lot of data so shared preferences may not suit this situation, i would advise using Room Database.

https://developer.android.com/training/data-storage/room/index.html

JakeB
  • 2,043
  • 3
  • 12
  • 19
0

From the documentation:

If you'd like to keep some data temporarily, rather than store it persistently, you should use the special cache directory to save the data. Each app has a private cache directory specifically for these kinds of files. When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your app, these files are removed.

If you need to cache some files, you should use createTempFile(). For example, the following method extracts the file name from a URL and creates a file with that name in your app's internal cache directory:

private File getTempFile(Context context, String url) {
    File file;
    try {
        String fileName = Uri.parse(url).getLastPathSegment();
        file = File.createTempFile(fileName, null, context.getCacheDir());
    } catch (IOException e) {
        // Error while creating file
    }
    return file;
}

Files created with createTempFile() are placed in a cache directory that's private to your app. You should regularly delete the files you no longer need.

To read an existing file, call openFileInput(name), passing the name of the file.

You can get an array of all your app's file names by calling fileList().

References:
Data and File Storage Overview
Cache File Storage

SharedPreferences is more suited for key value pairs not for dumping json.
SQLite is suited for bigger databases.

  • Internal file storage: Store app-private files on the device file system
  • External file storage: Store files on the shared external file system
  • This is usually for shared user files, such as photos.
  • SharedPreferences: Store private primitive data in key-value pairs.
  • Databases: Store structured data in a private database.
Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
  • Woah dude! You explained it so well and cleared all the doubts. But just one more thing, so should I just make a method and call it every time an article is opened? –  Jul 24 '19 at 09:35
  • When you receive the json from api call, like for article as you mentioned, you can call a function that saves the json in the temporary file created by above function. Then when the api call fails, you call a function to read the data from tempfile and parse it into json. – Zohaib Amir Jul 24 '19 at 09:39
  • You can use FileInputStream or FileOutpuitStream and pass the File object in the constructor. Make sure to do this in Asynchronously (in Async Task etc) though since you wouldn't want to pause your app when cache is being handled. – Zohaib Amir Jul 24 '19 at 09:41
  • Do yoh have any online tutorial course on youtube/udemy anywhere else? I would definitely like to follow up. Thanks. –  Jul 24 '19 at 09:43
  • I don't know about any good video tutorial, but [this answer](https://stackoverflow.com/a/33572815/3266367) will give you the easiest solution you can get for reading and writing to file :) – Zohaib Amir Jul 24 '19 at 09:46
  • @SamarpitShrivastava as for doing it in background(asynchronously), it would be best to follow the [official documentation](https://developer.android.com/reference/android/os/AsyncTask) – Zohaib Amir Jul 24 '19 at 09:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196912/discussion-between-zohaib-amir-and-samarpit-shrivastava). – Zohaib Amir Jul 24 '19 at 09:49
-1

If you refer the android life cycle you would end-up getting to know that as soon as app is killed non of it's instance nor any method can run in background.

To solve this you can implement broadcast for the same.

https://developer.android.com/guide/components/broadcasts

https://developer.android.com/guide/components/broadcast-exceptions

If you found this helpful, give a thumbs up! so that others can know about the correct answer!

Thank you!