PersistentObject Blackberry
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;
}
开发者_StackOverflow中文版 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?
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().
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.
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.
精彩评论