BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-24-2008, 09:26 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default save my vector persistent

Please Login to Remove!

hi,
i have a vector. each item of this vector is an object of an own class (always the same calss).

now i want to save this vector and reaload it the next time my application is started.

is teher a way to do this?

thanks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 11-24-2008, 09:38 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

Check out the PersistentStore, and the Persistable interface.
Offline  
Old 11-28-2008, 07:03 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, ok i have written my own class, which should save a vector.
i run my application -> load non existing data -> nothing happens -> OK
i gererate some data to save it > save the data -> OK -> quit the application
i run my application again -> load existing data -> data is shown -> OK
i turn of my blackberry (remove the batterie) -> boot it -> run my application -> load da... what? where is my data? the vector is null. what's wrong?

here is my complete class:
PHP Code:
class clsSave {
    private static 
Vector _data;
    private static 
PersistentObject store// = PersistentStore.getPersistentObject(0x444ed4bfea1c3f22L);
    
    
clsSave() { // constructor
        
if (store == nullstore PersistentStore.getPersistentObject(0x444ed4bfea1c3f22L);
        
_data = new Vector();
    }
    
    public 
void save(Vector _liste) { // gets a vaector, and should save it
        
if (store == nullstore PersistentStore.getPersistentObject(0x444ed4bfea1c3f22L);
        
synchronized(store) {
            
store.setContents(_liste);
            
store.commit();
        }
    }
    public 
Vector load() { // get the persistent data and returns a vector
        
synchronized(store) {
            
_data = (Vector)store.getContents();
        }
        return 
_data;
    }

so why is teh data los when i turn off my BB?

thanks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 11-28-2008, 11:23 AM   #4 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

seems strange. did you try to actually put something into the vector? just a guess, maybe the system GCs the store because there is only an empty vector in it.
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 12-01-2008, 02:58 AM   #5 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
as i wrote in my last post, i store some data into the vector, quit my programm and restart it. then the data is loaded correctly. even if i shutdown the BB then the data is lost.

any ideas?

thanks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 12-01-2008, 09:14 AM   #6 (permalink)
New Member
 
Join Date: Dec 2008
Model: 666
PIN: N/A
Carrier: T-Eufel
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I do the same thing. As long as the battery stays charged in the device the data are there. If you remove the battery and insert it again the data is lost.

I'm looking for a solution for quite a while.

At the moment I have additional problems with commit() - it crashes most of the time but the reason for this is probably my own fault.
Offline  
Old 12-02-2008, 02:57 AM   #7 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
if i store a string array with a fixed size then the data is saved correctly, also if i remove the batterie.
maybe the problem is, that a vector can have different sizes. maybe i just have to say: dear BB, give me space for 15 elements, also when my vector has just 4 elements.

is there a way to try it this way? ^^

thanks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 12-02-2008, 03:59 AM   #8 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

try the "static way"

Code:
class clsSave {
    private static Vector _data;
    private static PersistentObject store;

    static
    {
        store= PersistentStore.getPersistentObject(0x444ed4bfea1c3f22L);
        synchronized (store)
        {
            _data= (Vector) store.getContents();
            if (_data== null)
            {
                _data= new Vector();
                store.setContents(_data);
                store.commit();
            }
        }
    }

    
    clsSave() { // constructor
    }
    
    public void save(Vector _liste) {
        _data = _liste;
        synchronized (store)
        {
            store.setContents(_data);
            store.forceCommit();
        }
    }
    public Vector load() {
        return _data;
    }
}
This should work however it might be not necessary to replace the Vector object in the store by a new one. In my application I define getter and setter for the vector's element not for the vector itself.
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 12-02-2008, 04:56 AM   #9 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
it is of no importance how i access to the vector,or? the main problem is to save it permanently.

your way returns the same result as my one.
i can restart the application an the data is loaded. but if i remove the battery then teh data is lost :(

hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 12-02-2008, 05:05 AM   #10 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

try to copy all elements of _liste into _data beacuse
_data = _liste

sets only the reference to the new vector without making a deep copy

so try that:
Code:
    public void save(Vector _liste) {
        _data.removeAllElements();
        for (Enumeration e = _liste.elements(); e.hasMoreElements();)
        {
              _data.addElement(e.nextElement());
        }
        synchronized (store)
        {
            store.forceCommit();
        }
    }
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)

Last edited by Ivanov : 12-02-2008 at 05:06 AM.
Offline  
Old 12-02-2008, 06:59 AM   #11 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

at the moment i have try it with this one here:
PHP Code:
public void save(Vector _liste) {
        try {
            if (
_data == null_data = new Vector();
            
this._data.removeAllElements();
            for (
Enumeration e _liste.elements(); e.hasMoreElements();){
                
_data.addElement(e.nextElement());
            }
    
            if (
store == nullstore PersistentStore.getPersistentObject(0x444ec4babc0c3f52L); 
            
synchronized (store){
                
store.setContents(_data);
                
store.commit();
            }
        } catch (
Exception ex) {System.out.println("error 0x5540: "+ex.toString());}
    } 
and i get this error:
error 0x5540: net.rim.device.api.system.NonPersistableObjectExce ption
but i don't have any idea. store and _data are not null.

hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 12-02-2008, 07:13 AM   #12 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

a question in between:
what do you save in your _data vector?

if it's your own class it has to implement the Persistable interface as well.
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 12-02-2008, 08:03 AM   #13 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
each item of my _data vector is one of my own class (see first post).

how doi implement the persistable interface to my own class?

hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 12-02-2008, 08:22 AM   #14 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
import net.rim.device.api.util.Persistable;

class MyPersistableClass implements Persistable
{
}
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 12-02-2008, 08:33 AM   #15 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Ivanov View Post
Code:
import net.rim.device.api.util.Persistable;

class MyPersistableClass implements Persistable
{
}
jepp, i found it my self, was reallay an easy step ^^

i have tried this one, and i don't know what i should say....

it works

thanks alot for your help
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 12-03-2008, 04:58 AM   #16 (permalink)
New Member
 
Join Date: Dec 2008
Model: 666
PIN: N/A
Carrier: T-Eufel
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahhh! This is the solution. If it is implemented right there are no crashes and the data is saved. Real nice from the compiler that it doesn't even warn you if you forgot the implementation...

Thanks a lot.

Thread could be closed.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.