Jump to content
McObject Forums
Mikkel

NO DATA when open DB

Recommended Posts

Mikkel    0

Hello,
i want to try to use perstdb inside an android app, but first I have to test if it is good for  my case. Therefore I write one Testapp. Now I have a problem. I write a  class called "PerstDataManager" which is used to interact with the db. My Problem is always when i call the constructor of these class I lost all data. 
When I call the constructor  I can write and read from the database. But with the next start of the App I have to call the constructor again, and then the whole Data is lost.
I think I make something wrong inside the code of my constructor.

public PerstDataManager(Context ApplicationContext)
{
   this.ApplicationContext = ApplicationContext;
   String Filepath = this.ApplicationContext.getFilesDir().getAbsolutePath();
   // Get instance of Perst storage
       db = StorageFactory.getInstance().createStorage();
       // Open the database with given database name and spacified page pool (database cache) size
       db.open(Filepath+"/" + databaseName, pagePoolSize);
       // There is one root object in the database. 
       root = (PerstRootClass)db.getRoot();
       if (root == null) 
       {
           Log.i(MainActivity.LogTag, "root == null");
           //Toast.makeText(this.ApplicationContext, "root Object is null", Toast.LENGTH_LONG).show();
           // if root object was not specified, then storage is not yet initialized
           // Perform initialization:
           root = new PerstRootClass(db);// ... create root object
           // and register new root object
           db.setRoot(root);
       }

       //Toast.makeText(this.ApplicationContext, "Objects stored:" + (root.intKeyIndex.size()), Toast.LENGTH_SHORT).show();
}

 

Can somebody help me to find my mistake?
I will attach the whole class at this post.

 

PerstDataManager.java

Share this post


Link to post
Share on other sites
perstmco    0

It is not clear whether database is closed or not.
There is no commit in the PerstDataManager constructor, so there is no assurance that initialization of database is actually persisted.
There is close method in PerstDataManager class, but it is not clear whether it is called (elsewhere) or not.

Share this post


Link to post
Share on other sites
Mikkel    0

Hy,

thanks for your reply. I will try if adding a commit to my constructor will fix my problem. I get back tomorrow to inform you if it worked.

The close method get called before the app get closed.

 

 

Share this post


Link to post
Share on other sites
Mikkel    0

Hello,

first I changed my code an add  two submit to the constructor of my datamanager.   I will attach the new code.   I will also attach the code of my testfunction where you can see the datamanager is closed at the end of this function. Before this close, everything looks good.


But also after adding the commit, it doesn't work correct. Because of this I tried to debug my app with the  android studio debugger. There I found some interesting. If I  look into the root object  after insert everything is stored fine. Then the db get closed. If I reopen the db (call again datamanager = new PerstDatamanager...) I can see some of the stored data is correct and some is wrong.
I made screenshots of this. May be these screenshots help you to find the mistake.
The problem is that the value of datacount and lastlist is not stored after reopen the datamanager.

I will attach the code of the datamanager, the code of my test function inside my activity and three pictures of my debugger states.

Some information about the attached pictures.
Perst Error before first insert.JPG:  Shows the state after first start of the app. Everything looks fine.


Perst error, everything is fine after insert.JPG: shows the state after insert some Data. Everything looks fine, datacount is 10000 and lastlist is 1.


Perst error inside Constructor.JPG: shows the state after second call of my constructor. You can see the index sizes are ok, the name is ok, strkey is ok.....but datacount is 0 and lastlist is 0

Why this data get lost? Where is my mistake?

 

 

 

PerstDataManager

PerstTestFunction in Activity
Before first insert:

Perst Error before first insert.JPG

After First Insert:

Perst error, everything is fine after insert.JPG

After second call of PerstDataManager Constructor:

Perst Error inside Constructor.JPG
 

The Problem is that datacount  = 0 and lastlist = 0

I don't know why.

Edited by Mikkel
wrong order of pictures inside my post

Share this post


Link to post
Share on other sites
perstmco    0

Ok, from the screenshots we see that root object is correctly stored, but not the Fahrt  class.
Please send the definition of this class and code inserting it in the index.

Share this post


Link to post
Share on other sites
Mikkel    0

hello an thanks perstmco,

here is the code of my class Fahrt:

public class Fahrt extends Persistent
{
    private String fahrtName;
    private String fahrzeug;
    private String zeitstempel;
    private long datacount;
    private long lastlist;
    public String strKey;

    public Fahrt(String fahrtName, String fahrzeug, String zeitstempel)
    {
        this.fahrtName = fahrtName;
        this.fahrzeug = fahrzeug;
        this.zeitstempel = zeitstempel;
        this.strKey = fahrtName;
        this.datacount = 0;
        this.lastlist = 0;
    }
    public Fahrt()
    {

    }

    public String getFahrtName()
    {
        return this.fahrtName;
    }

    public String getFahrzeug()
    {
        return this.fahrzeug;
    }

    public String getZeitstempel()
    {
        return this.zeitstempel;
    }

    public void setFahrtName(String name)
    {
        this.fahrtName = name;
    }

    public void setFahrzeug(String fahrzeug)
    {
        this.fahrzeug = fahrzeug;
    }

    public void setZeitstempel(String zeitstempel)
    {
        this.zeitstempel = zeitstempel;
    }

    public void setDatacount(long newcount)
    {
        this.datacount = newcount;
    }

    public long getDatacount()
    {
        return this.datacount;
    }

    public void setLastlist(long number)
    {
        this.lastlist = number;
    }

    public long getLastList()
    {
        return this.lastlist;
    }

    public String toString()
    {
        return this.strKey + " | " + this.fahrtName + " | " + this.fahrzeug + " | " + this.zeitstempel;
    }
}

It gets added to the index in my Datamanager in the insertDataInTablemethod inside my PerstDataManager.java. I posted this class before.

 

Why I can't open my own attached Datamanager.java in this forum?

Edited by Mikkel

Share this post


Link to post
Share on other sites
perstmco    0

>> Why I can't open my own attached Datamanager.java in this forum?

Sorry. The forum software was recently upgraded, and we're trying to locate the administrative option that controls this.  I'll forward the file to the developers on your behalf.

Share this post


Link to post
Share on other sites
Mikkel    0

>> Why I can't open my own attached Datamanager.java in this forum?

Sorry. The forum software was recently upgraded, and we're trying to locate the administrative option that controls this.  I'll forward the file to the developers on your behalf.

Can you forward the file to me too? Because I accindentally delete the file on my pc.

Can you send me this file to my mailaddress perstmco?

Edited by Mikkel
forgot something

Share this post


Link to post
Share on other sites
perstmco    0

Java doesn't have behavioral reflection, so it is not possible to detect when an object is updated. The programmer has to explicitly mark objects as updated using the Persistent.modify() method, or explicitly save a modified object using the Persistent.store() method. The first method is more efficient and convenient because an object can be updated multiple times. In your code, the Fahrt object is changed without notifying Perst that it was changed, for example:

 

fahrtx.addDatacount((list_tofill.size() * -1));

 

There is no addDatacount method in the version of Fahrt class you publish in the forum, but there is setDatacount which seems to do similar work:

public void setDatacount(long newcount)
    {
        this.datacount = newcount;
    }

To fix the problem you need to add an invocation of the modify() method to this (and all other) methods:

 

public void setDatacount(long newcount)
    {
        this.datacount = newcount;
        modify();
    }

Share this post


Link to post
Share on other sites
Mikkel    0

Thank you very much perstmco. Addinf moodify(); to my setterMethods solved my problem. Very nice to get such a nice support from you.

Can I close this topic?

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×