Storage information lost on app exit.

96 views
Skip to first unread message

Sadart Abukari

unread,
Mar 3, 2014, 2:52:20 PM3/3/14
to codenameone...@googlegroups.com
I am definitely doing something wrong here but can't figure it out. Can anyone assist?
I am saving a Vector<Hashtable> to storage.
When I exit the app and start the Storage gets lost and the data has to be fetched from the server again. Storage is only available when the app is not exited. The method I am using is right here
    private void fetchTeamInfo() {
           Vector<Hashtable> postForRenderers = (Vector<Hashtable>) Storage.getInstance().readObject("TeamData")
        if (postForRenderers != null) {
            Dialog.show("TeamData Exists", "TeamData Exists", "Ok", "Cancel");
        } else {
            ConnectionRequest connectionRequest;
            connectionRequest = new ConnectionRequest() {
                Vector vTeamsData = null;

                @Override
                protected void readResponse(InputStream input) throws IOException {
                    JSONParser json = new JSONParser();
                    Hashtable h = json.parse(new InputStreamReader(input));
                    vTeamsData = (Vector<Hashtable>) h.get("root");
                }

                @Override
                protected void postResponse() {
                    if (vTeamsData.isEmpty()) {

                    } else {
                        boolean written = Storage.getInstance().writeObject("TeamData", vTeamsData);
                        if (written) {
                            Dialog.show("TeamData", "TeamData Written", "Ok", "Cancel");
                        }
                        System.out.println("TeamData Written! " + written);
                    }
                }

            };
            connectionRequest.setUrl("http://sadartabukari.com/dropapi/api/views/teams_information");
            connectionRequest.setPost(false);
            connectionRequest.removeAllArguments();
            NetworkManager.getInstance().addToQueueAndWait(connectionRequest);
        }
    }

Shai Almog

unread,
Mar 4, 2014, 3:06:25 AM3/4/14
to codenameone...@googlegroups.com
Is this happening on the simulator.
Do you have any output to the console after invoking the writeObject method?

Sadart Abukari

unread,
Mar 4, 2014, 4:20:24 AM3/4/14
to codenameone...@googlegroups.com
It is happening both on emulator, 2 HTC android devices and 1 other samsung Android device. Each of them have SD card which store data for other apps.
I thought the                      
  System.out.println("TeamData Written! " + written);
is the cause so I removed it but Storage still gets lost on app exit.
I used the debugger to check the Storage like this
                                   Vector<Hashtable> postForRenderers = (Vector<Hashtable>) Storage.getInstance().readObject("TeamData");

 // Breakpoint below     
System.out.println("TeamData Written! " + written);

and postForRenderers  shows the Vector data fetched Storage.
But when the app exists Storage still gets lost.

Shai Almog

unread,
Mar 4, 2014, 2:25:44 PM3/4/14
to codenameone...@googlegroups.com
Storage caches data so the fetch while the app is running will bring the data from cache even if write failed. You can clear the cache which will show the failure immediately.
Is there no printout to console after the write operation?

Sadart Abukari

unread,
Mar 4, 2014, 3:09:05 PM3/4/14
to codenameone...@googlegroups.com
Below is the update of my method which fetches the data. I clear cache at the beginning of the method and the first time when data is fetched from the server. There's no print out to the console after write operation. Breakpoint I set shows postForRenderer  in is fetched from storage since cache clearing is done before that
Vector<Hashtable> postForRenderer = (Vector<Hashtable>) Storage.getInstance().readObject("TeamData");
When app exits and started and the method is run, nothing is got from storage so data has to be refetched from the server.

    private void fetchTeamInfo() {
        Storage.getInstance().clearCache();

        Vector<Hashtable> postForRenderers = (Vector<Hashtable>) Storage.getInstance().readObject("TeamData");

        if (postForRenderers != null) {
            Dialog.show("TeamData Exists", "TeamData Exists", "Ok", "Cancel");
        } else {
            ConnectionRequest connectionRequest;
            connectionRequest = new ConnectionRequest() {
                Vector vTeamsData = null;

                @Override
                protected void readResponse(InputStream input) throws IOException {
                    JSONParser json = new JSONParser();
                    Hashtable h = json.parse(new InputStreamReader(input));
                    vTeamsData = (Vector<Hashtable>) h.get("root");
                }

                @Override
                protected void postResponse() {
                    if (vTeamsData.isEmpty()) {

                    } else {
                        boolean written = Storage.getInstance().writeObject("TeamData", vTeamsData);
                        if (written) {
                            Dialog.show("TeamData", "TeamData Written", "Ok", "Cancel");
                        }
                        Storage.getInstance().clearCache();
                        Vector<Hashtable> postForRenderer = (Vector<Hashtable>) Storage.getInstance().readObject("TeamData");


                        System.out.println("TeamData Written! " + written);
                    }
                }

            };
            connectionRequest.setUrl("http://sadartabukari.com/dropapi/api/views/teams_information");
            connectionRequest.setPost(false);
            connectionRequest.removeAllArguments();
            NetworkManager.getInstance().addToQueueAndWait(connectionRequest);
        }
    }


Sadart Abukari

unread,
Mar 4, 2014, 3:31:22 PM3/4/14
to codenameone...@googlegroups.com
Feeling sheepish.
I had a Storage.getInstance().clearStorage(); in my initVars method. Which was the culprit. I removed it and everything works. Thanks for drawing my attention to the clear cache since it let me revise where I might be clearing anything storage in my code.

    protected void initVars(Resources res) {
        Storage.getInstance().clearStorage();// removed
        vGlobalNewsPost = new Vector<Hashtable>();
        vTeamsInfo = new Vector<Hashtable>();
        startPage = "1";
        Util.register("UserObject", UserObject.class);

}

On Tuesday, March 4, 2014 7:25:44 PM UTC, Shai Almog wrote:

sont...@gmail.com

unread,
Mar 20, 2018, 2:54:39 PM3/20/18
to CodenameOne Discussions
Hi all,

I'm also getting same issue.
I'm working on the Swiftnotes code in Eclipse, the data is lost after exiting the app, I don't use clearStorage() anywhere, the data has been written/read to/from storage successfully.
this issue is happening on both simulator and my Android phone (Galaxy S8) .

I tried to clear cached of the app installed on my phone, even allowed permissions on Storage but no luck.
Do I need to do anything else like configurations on phone, code changes, etc ?

Thanks!
Thuan.

Shai Almog

unread,
Mar 21, 2018, 12:12:32 AM3/21/18
to CodenameOne Discussions
Hi,
do you see any exception in the console during writing or during loading in the simulator?

sont...@gmail.com

unread,
Mar 21, 2018, 9:44:44 AM3/21/18
to CodenameOne Discussions
Thanks Shai!

I don't see any exception during read/write in simulator. Everything is working fine, until I exit/close the app then I lost saved data.

I use the sourcecode on below github, however I added some code changes to process json, send requests to remote url, etc:

Shai Almog

unread,
Mar 22, 2018, 12:53:31 AM3/22/18
to CodenameOne Discussions
I just verified that swiftnotes works for me. I'm guessing you made changes to read/write object or you added new/unsupported object types into the data.

This should trigger a printout in the console in the bottom of the IDE when you are running either when you save a note or when you try to load on startup.

sont...@gmail.com

unread,
Mar 23, 2018, 1:30:22 AM3/23/18
to CodenameOne Discussions
Thank Shai!

The issue has gone now..

After clean up my code close to original now I can see the error in console, I've tried to debug below read/write methods .

So below line is the cause
out.writeUTF(updated_date);
 
-> when I read: updated_date = Util.readUTF(in);
           it caused EOFException.
 
Then I changed to :
Util.writeUTF(updated_date, out);

it worked fine.

Thanks!
Thuan.
Reply all
Reply to author
Forward
0 new messages