1

How can you easily check whether your app has persistentObjects? Right now I'm using the following:

public boolean needsFirstTimeInit() {
    PersistentObject persistentObject = getPersistentObject(Settings.TABLE_USERS);
    Vector vector = (Vector) getVector(persistentObject);
    if(vector.size()<=0){
        return true;
    }
    return false;
}

The negative here is that I'm asking data from a table I know that has to exists, and if it exists I assume the tables haven't been initialized. Is there a better way of doing this?

Vincent
  • 6,058
  • 15
  • 52
  • 94

3 Answers3

1

The code you've shown isn't quite right. There's a simple example in the PersistentStore BlackBerry API docs that shows you what you need to do.

Basically you call getContents() on the PersistentObject you fished out of the store. If it's null, there's nothing stored and you need to initialize it to something using setContents().

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Eric Giguere
  • 3,495
  • 15
  • 11
  • yes but then you still need to specify a table name. Isn't there a way of saying 'If no persistentObjects exist' ? – Vincent Feb 01 '11 at 13:28
  • Not sure what you mean by "table name", this is not a relational database. Persistent objects are identified using a unique key. If you want to know if a persistent object exists or not, there is no API for that. You can only get persistent objects (which will create it if it doesn't exist) or destroy them. – Eric Giguere Feb 01 '11 at 14:12
  • I use the hashcode of a tablename to look up objects (I probably should have made that clear). – Vincent Feb 01 '11 at 16:09
  • Good, I was just worried that you were confusing things. I hope I've answered your questions about persistent objects. It's really quite a simple API. – Eric Giguere Feb 01 '11 at 16:17
0

A specific Persistent Object isn't associated with a specific app, so it doesn't make sense to talk about your app's persistent objects.

Any app that knows the GUID you used to get your PersistentObject can pass that same GUID to PersistentStore.getPersistentObject to get the same object back. This means GUID that you use for PersistentStore.getPersistentObject should be unique across all apps on the BlackBerry (in practice it's difficult to guarantee this). So if you use a given GUID and get back a persistent object with non-null contents, it could be that your app has saved it (most likely, given the small likelihood of two apps using the same 64-bit GUID) or it could be that some other app has saved an object with the same GUID.

In practice, most apps don't bother with any kind of check that they got back the expected object for a given GUID, and it doesn't cause problems. But it's something to be aware of for that one occasion where you get a weird bug because of a GUID collision.

Anthony Rizk
  • 3,666
  • 20
  • 21
  • Not quite true... persistent objects are associated with apps in the sense that if you persist a class that is part of your app and you delete the app, the persistent object is deleted. As you mention, the GUIDs are shared across the device and it's one way for apps to share data and there is a remote possibility of name collision. – Eric Giguere Feb 01 '11 at 16:16
  • True - and I thought about mentioning that in my answer but figured it might be getting off topic a bit. – Anthony Rizk Feb 02 '11 at 14:46
0

The following solution worked best for me:

PersistentObject is considered as a table and contains a Vector(filled with objects).

I made a vector with all hashcodes of the tableNames. (v1) I made a hashtable (hashcode tablename, PersistentObject); (h1) On every startup I check if all hashcodes in v1 have a value (persistentObject) in h2. If not, I initialise the persistentObject and put it in the hashtable.

Vincent
  • 6,058
  • 15
  • 52
  • 94