Maintaining an array of data in Cordova/Phonegap that persists when the app is closed and reloads when reopened

I have set up an array in my application that looks like this [{pseudo: "test", id: 0}, {pseudo: "Lucia", id: 2}]

Is there a way to preserve this array even when the app is closed? Additionally, I would like to access the array directly upon reopening the app, rather than when it is running in the background.

UPDATE: I attempted to achieve this by:

Storage :

 var friend = ({
     idx: id
 });
 var friend_json = JSON.stringify(friend);
 sessionStorage.setItem("friend", friend_json);

The read operation only occurs the first time I open the app (placed right after my controller starts), but I receive a null value in the console log.

var friendStorage = [];
var storageFriendsNotified = window.localStorage;
var friend_json = sessionStorage.getItem("friend");
var friend = JSON.parse(friend_json);

console.log(angular.toJson(friend));
console.log(friend);

friendStorage.push(friend);

console.log(friendStorage);
console.log(angular.toJson(friendStorage));

UPDATE 2: $scope.isChecked is used for ng-class, I am trying to access the id of my json, but it appears in this format [[{"pseudo": pseudo, "id": id}]] ... friendsNotified[i].id does not seem to work

var friendsNotified = [];
$scope.isChecked = [];

var storageFriendsNotified = window.localStorage;
var friend_json = window.localStorage.getItem("info");
var friend = JSON.parse(friend_json);

if (friend != null){
    friendsNotified.push(friend);
    if (friendsNotified.length > 0) {
        for (var i = 0; i < friendsNotified.length; i++) {
            console.log(friendsNotified[[i]].id);
            $scope.isChecked[friendsNotified[i].id] = "fa fa-check-circle-o pull-right";
        }
    }
}

Answer №1

Consider using localStorage in place of sessionStorage

 var user = ({
         id: userId
     });
     var user_json = JSON.stringify(user);
     window.localStorage.setItem("user", user_json);

Additionally, you can use the code below

    var userStorage = [];
    var storageUsers = window.localStorage;
    var user_json = window.localStorage.getItem("user");
    var user = JSON.parse(user_json);

    console.log(angular.toJson(user));
    console.log(user);

    userStorage.push(user);

    console.log(userStorage);
    console.log(angular.toJson(userStorage));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

establishing a "string" array within an array of structures and assigning it a value

Within my program, there is a struct that contains information such as name and age. The issue lies within the displayRecords() function in the main section where I am attempting to bubble sort an array of these structs based on age. While I have been succ ...

What is the best way to form a unique list within a dictionary in Python while avoiding duplicate keys?

I need to generate an array containing id_type_sale and sale_type_description from object_sale_types, focusing on the keys email, date, order_id, and store without any duplicates (assuming the data is the same). Input { "105": [ { &q ...

Exploring the world of PPM image formats

: Tag P6 650, 652 255 The tag P6 signifies a PPM image format. The subsequent values represent the width and height of the image, followed by the maximum pixel value. After this header information comes binary pixel data with three bytes for color (red ...

Revamp State before redirecting using React Router Dom

In my current project, I am facing an issue with redirecting after updating the state successfully. The technologies I'm using include React 16.12.0 and React Router DOM 5.1.2. Here's the scenario: I have a button that, when clicked, should updat ...

Tips for incorporating theme colors in the "color" prop when employing MaterialUI components

Trying to utilize the colors from my Mui palette as values for the color property in this way: For example: <Tabs indicatorColor="primary.mainGradient" > <Tab /> </Tabs> However, this approach does not seem to wo ...

Remove duplicate values from a multidimensional array

Hey everyone, I'm facing an issue and haven't been able to find a solution online. I may have missed something, but the answers I came across didn't quite address my problem. Here's the situation: ["acList"]=> array(356) { ...

After successfully uploading a file, refresh the file list using AngularJS, DropzoneJS, and JSON

In my "file manager page," I am using DropzoneJS for uploading files and AngularJS ng-repeat to display all the saved files. The file list is retrieved through an ng-controller that uses an http.get request to fetch a JSON file with the file list. When Dr ...

The correct method for accessing descendants in THREE.js in the latest version, r68

As of the release r68, the getDescendants() method has been removed from the THREE.Object3D API. How should we now achieve the same functionality without any warning message being provided? ...

The function persists in outputting a true result, despite the fact that it is expected to output

Currently, I am working on a NextJS project where I have a client-side form. I've been attempting to implement validation for the form by creating a separate function called validateForm(). However, no matter what input is provided, the function alway ...

Developing a NASM assembly character array for utilization in syscall 59

My objective is to create a file that executes the command: /bin/bash -c "echo hello; ls -la" section .text global _start _start: BITS 64 jmp short two one: pop rbx mov [rbx+34], rbx xor al, al ...

Controlling multiple bx-sliders with a single pager using only JavaScript

Is there a way to control 3 sliders using the same pager? I attempted to use the following code but it did not work: <script language="javascript"> $('.mobile_left_mble,.mobile_right_mble,.text_btn').bxSlider({ //pagerCustom: '#bx- ...

Making React function properly on GitHub Pages

I have followed all the steps and even consulted multiple tutorials that all say the same thing, but when I try to run "npm run deploy" I encounter an error and nothing happens. This is the error message: [email protected] deploy A:\Documents&bs ...

Utilizing Shadow Root and Native Web Components for Seamless In-Page Linking

An illustration of this issue is the <foot-note> custom web component that was developed for my new website, fanaro.io. Normally, in-page linking involves assigning an id to a specific element and then using an <a> with href="#id_name&quo ...

What is the best way to organize data that has been refined?

I have a dataset that can be filtered to display only the rows with "Color" data when selecting "Color Only" from the sort list. Here is the code I am using: <label>Sort By:</label> <select ng-model="orderProp" class="form-control" tabindex ...

Having trouble with the jQuery slideUp function?

Exploring some jQuery features for a trial run, I stumbled upon an unusual behavior. Perhaps someone in this forum can provide some insight. Keep in mind that this is all just test code, so please overlook any messy aspects. Currently, I am working with ...

Aligning the second heading alongside pictures and a lightbox

My issue is that I am struggling to center the "See More" link directly under each title link. Currently, it is pushed to the right and I can't seem to find a solution. Any help with this problem would be greatly appreciated. I attempted using display ...

Utilizing JavaScript to handle the value from a selected form

My form is quite simple: <form id="signup"> <div class="form-group"> <input type="text" name="email" placeholder="Email" id="email" required> </div> <div class="form-group"> <input type= ...

Obtain the beginning and ending positions of a substring within a larger string

As a beginner in the world of AngularJS and web development, I am faced with a challenge. I have a specific string that is currently selected on a tab, like so: var getSelectedText = function() { var text = ""; if (typeof window.getSelection !== "un ...

Barrier Preventing My Access to the Page

Currently, I am working on a vue page that includes a navigation-guard. This particular page serves as an 'Update Details' section where I have implemented the use of beforeRouteLeave. The purpose of this setup is to display a warning modal if an ...

Wcf Service does not define Angular JS methods

I am utilizing a WCF Service within an AngularJS application. The WCF Service is functional, and I am attempting to display a list of user records from a SQL database. However, upon running the application, I encountered the following errors: angular.js: ...