Search code examples
storagenativescriptapplicationsettingsbase

NativeScript best way of saving favorites


I am looking for correct and decent way of storing favorites articles in my app to phone storage. I built the app with Web shop articles listed and I would like to allow users to save favorite articles to the "favorite" list.

I have tried application-settings plugin and I am not sure is this a good way of doing it because keys needs to be predefined. (I am not really sure). Is there a some kind of JSON style data storage which does work on Android and iOS? - It would be great because I could sync it to Cloud storage at some point.

I need a easy way for manipulating data, add, remove, clear the list. Something what will not impact app performance. Cheers guys.


Solution

  • It's entirely up to you to choose one of below. I would say you should be good with application-settings itself.

    application-settings is still a quick and good option.

    function getFavorites() {
      return applicationSettings.getString("favorites", "[]");
    }
    
    function setFavorites(data) {
      return applicationSettings.setString("favorites", JSON.stringify(data));
    }
    
    let data = getFavorites();
    
    // Add new
    data.push({
        id: "NewFavId",
        ... // other attributes
    });
    
    // Delete existing one
    data = data.filter((item) => item.id === "IdToDelete");
    
    
    // Update application settings
    setFavorites(data);
    

    nativescript-localstorage is another similar option which internally uses JSON files to store data.

    Speaking of storing data on device, nativescript-sqlite is also there but you may not need this for simple data operations like yours Or nativescript-secure-storage which is again another option but only to be used for storing highly sensitive information.