【问题标题】:Persistent Store in Java Given Runtime StoreJava中的持久存储给定运行时存储
【发布时间】:2012-02-11 09:57:08
【问题描述】:

我正在尝试创建一个持久且共享的变量,该变量将跟踪黑莓应用程序中用户可用的通知数量。此号码显示在主屏幕上,即使在设备关闭后也应保留,直到他们自己检查应用程序,然后重新设置号码。我一直在使用单例在后台进程和 UI 应用程序本身之间共享变量:

import net.rim.device.api.system.RuntimeStore;

public class IconManager {
    private static IconManager _instance;
    private static final long GUID = 0xab4dd61c5d004c18L;
    private int iconCount;

    // constructor
    private IconManager() {
        iconCount = 0;
    }

    public static IconManager getInstance() {
        if (_instance == null) {
            _instance = (IconManager) RuntimeStore.getRuntimeStore().get(GUID);
            if (_instance == null) {
                IconManager singleton = new IconManager();

                RuntimeStore.getRuntimeStore().put(GUID, singleton); 
                _instance = singleton;
            }
        }
        return _instance;
    }

    public int getCount() {             
        return iconCount;
    }

    public void setCount(int count) {      
        iconCount = count;
    }
}

我一直主要使用这个网站来尝试找出 Persistent store 部分:http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747

在给定上述运行时存储的情况下,是否有替代方案来实现持久存储?我最初想使用 Blackberry 示例中的代码,但我对如何执行此操作感到困惑。从另一个线程用户 mparizeau 写了以下内容:

persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
synchronized (persistentCount) { 
    if (persistentCount.getContents() == null) { 
        persistentCount.setContents(new StoreInfo());
        persistentCount.commit(); 
    } 
}  
_data = (StoreInfo)persistentCount.getContents();

现在,当您想要更新它并保存到 PersistentStore 时,您可以使用以下内容:

_data.incElement();
synchronized(persistentCount) {
    persistentCount.setContents(_data);
    persistentCount.commit();
}

可以在上面的代码中以某种方式使用它吗?我对 java 和 BB 开发非常陌生,因此我们将不胜感激。

【问题讨论】:

  • 您到底需要什么?明确指定..

标签: java stored-procedures blackberry singleton persistence


【解决方案1】:

我认为您不想使用 RunTimeStore,因为您希望即使在设备关闭后也能保留信息。来自this page

运行时存储不是持久的。当您重新启动 BlackBerry 设备,运行时存储中的数据将被清除。

试试这样的:

public class IconManager {
    private static IconManager _instance;
    private final long GUID = 0xab4dd61c5d004c18L;
    private PersistentObject store;
    private int iconCount;

    private IconManager() {
        store = PersistentStore.getPersistentObject(GUID);
        synchronized(store) {
            if(store.getContents() == null) {
                store.setContents(new Integer(0));
                store.commit();
            }
        }
        iconCount = ((Integer)store.getContents()).intValue();
    }

    public static IconManager getInstance() {
        if (_instance == null) {
            _instance = new IconManager();
        }
        return _instance;
    }

    public int getCount() {             
        return iconCount;
    }

    public void setCount(int count) {      
        iconCount = count;
        synchronized(store) {
            store.setContents(new Integer(iconCount));
            store.commit();
        }
    }
}

【讨论】:

  • 嘿,非常感谢您的帮助!代码编译完美,但前提是我将 iconCount 声明更改为静态。现在我遇到了一个问题,在该声明上放置静态不会使代码工作,不幸的是,在这种情况下也没有保存 iconCount 变量。我认为这可能与将 iconCount 更改为 static 以进行编译有关。你知道解决这个问题的方法吗?
  • 同样从上面的代码中,单例不需要以某种方式存储吗?因为我认为单例是使变量在两个事件之间保持同步的原因,所以去掉它不会破坏共享公共变量的事件 1 和事件 2 之间的关系吗?
  • 糟糕,在这种情况下它实际上应该是静态的。但是我刚刚意识到,由于您将它用作单例,您实际上并不需要它是静态的,您应该能够将静态初始化程序中的内容移动到构造函数。我将更新代码以反映这一点。
  • 据我了解,您只希望存储 iconCount 变量,因此单例本身并不是“存储”的。将会发生的是,第一次在单例上调用 getInstance 时,它​​将转到构造函数并从 PersistentStore 加载 iconCount 的值。无论何时调用 setCount,iconCount 的值都会被保存到 PersistentStore。这有点像您正在从文件保存和加载,并且您从 GUID 变量中知道“文件”的名称,因此即使在重新启动后也可以找到“文件”。
  • 如果您关闭设备,它可能会进入休眠状态,并且运行时存储可能保持不变。但是设备重置(例如拔下电池)将清空运行时存储。
【解决方案2】:

Blackberry OS 5 及更新版本内置了 SQLite。您可以使用它来代替持久存储。 (它有一个类似 jdbc 的 API)。 BBOS 5 已经发布了很长一段时间了。

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2018-07-29
    • 1970-01-01
    相关资源
    最近更新 更多