java - RoboSpice 和 ORMLite - 访问数据

标签 java android rest ormlite robospice

我刚开始将 RoboSpice 用于一个新的 Android 应用程序。 RoboSpice 与 ORMLite 和 SpringAndroidSpiceService 一起使用以从 REST Web 服务读取 JSON。

目前,我设法:

  • 创建自定义 SpiceService
  • 很少请求从 WS 获取数据
  • 使用 ORMLite 将数据保存到数据库

... 感谢 "robospice-sample-ormlite" sample .

我的应用程序的主要部分(底部的问题):

基础 Activity

public abstract class BaseActivity extends RoboActivity {

    private SpiceManager spiceManager = new SpiceManager(MySpiceService.class);

    @Override
    protected void onStart() {
        super.onStart();
        spiceManager.start(this);
    }

    @Override
    protected void onStop() {
        if (spiceManager.isStarted()) {
            spiceManager.shouldStop();
        }
        super.onStop();
    }

    protected SpiceManager getSpiceManager() {
        return spiceManager;
    }

}

SplashActivity

public class SplashActivity extends BaseActivity {
    ...
    @Override
    protected void onStart() {
        super.onStart();
        ...
        getSpiceManager().execute(foosRequest, new Integer(0), DurationInMillis.ONE_HOUR, new FoosRequestListener());
        getSpiceManager().execute(barsRequest, new Integer(1), DurationInMillis.ONE_HOUR, new BarsRequestListener());

        // Start MainActivity after X seconds (handler + startActivity(intent...)
        ...
    }

    public final class FoosRequestListener implements RequestListener<Foos> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Toast.makeText(MainActivity.this, "failure foo", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRequestSuccess(final Days result) {
            Toast.makeText(MainActivity.this, "success foo", Toast.LENGTH_SHORT).show();
            String originalText = getString(R.string.textview_text);
            mLoremTextView.setText(originalText + result.getResult().iterator().next().getMoonrise());
        }
    }

    public final class BarsRequestListener implements RequestListener<Bars> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Toast.makeText(MainActivity.this, "failure bars", Toast.LENGTH_SHOR ).show();
        }

        @Override
        public void onRequestSuccess(final Months result) {
            Toast.makeText(MainActivity.this, "success bars", Toast.LENGTH_SHORT).show();
        }
    }
}

MySpiceService

public class MySpiceService extends SpringAndroidSpiceService {
    ...
    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        List<Class<?>> classCollection = new ArrayList< Class< ? >>();

        // Add persisted classe(s) to class collection
        classCollection.add(Foos.class); // Collection of "Foo"
        classCollection.add(Foo.class);
        classCollection.add(Bars.class); // Collection of "Bars"
        classCollection.add(Bar.class);

        // Init
        RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper(application, DATABASE_NAME, DATABASE_VERSION);
        InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory(application, databaseHelper, classCollection);
        cacheManager.addPersister(inDatabaseObjectPersisterFactory);

        return cacheManager;
    }

    @Override
    public RestTemplate createRestTemplate() {
        ...
        return restTemplate;
    }
}

应用程序由启动画面(SplashActivity)和其他 Activity 组成。启动画面在几秒钟后启动 MainActivity(目前只是一个计时器)。

我想做的是从启动画面下载所有数据(RoboSpice 已经缓存/将这些数据保存在 SQLite 数据库中),然后从其他 Activity 访问它。


问题 1: 是否可以在 SplashActivity 之外访问和操作数据库数据?如何(你能提供一些代码示例)?

The Wiki page关于“RoboSpiceDataBaseHelper”我不是很清楚:据说“RoboSpiceDataBaseHelper 用于管理数据库创建和版本管理”,但在下一段中指出某些方法允许“从数据库查询和检索数据”。在 RoboSpice 样本中,没有关于从其他 Activity 中检索数据的内容。如何利用 ORMLite 的强大功能?

我已经阅读了 this Google Groups topic来自 Snicolas:

Note that RoboSpice caching is meant to be transparent and is completely encapsulated in the SpiceService. It should be possilbe to access the cache system (in your case the databse) directly through a shared reference to it (shared by the service and your activities). Also, in that case, nothing will prevent side-effects like concurrent access or database inconsistency.

那么,怎么办? 我完全迷路了:(

编辑 03/01:由于我缺乏 RoboSpice 和 ORMLite 的能力,我很困惑如何实现这样的解决方案/如何自己做(至少有效)。我不知道如何使用/组合 RoboSpiceDatabaseHelper、InDatabaseObjectPersister、InDatabaseObjectPersisterFactory...


问题 2:

我的最后一个问题与数据访问无关(我带着这个话题来问)。

为什么 getSpiceManager().execute(barsRequest, new Integer(1), DurationInMillis.ONE_HOUR, null) 什么都不做(没有回调到 RequestListener)?事实上,就我而言(启动画面),我不需要回调。我能避免吗?


谢谢!

最佳答案

问题一:

您必须在您的服务和您想要访问它的地方之间共享 RoboSpiceDatabaseHelper

最干净的方法是使用像 RoboGuice 这样的注入(inject)框架。一个快速而肮脏的 hack 是为此实例创建一个静态提供者,或者甚至更肮脏和更快,使 RoboSpiceDatabaseHelper 成为单例。

---更新

在使用 RoboGuice 时,您可以:

创建您自己的 RoboSpiceDatabaseHelper 类并使用 @Singleton 对其进行注释:

@Singleton
public class MyRoboSpiceDatabaseHelper extends RoboSpiceDatabaseHelper {
...
}

在您的 spiceservice 和需要访问数据库的类中,您可以声明一个成员:

@Inject
RoboSpiceDatabaseHelper databaseHelper;

然后您将获得一个合适的单例,允许您共享数据库访问。

问题二:

如果您真的需要快速进行,只需创建一个 stub /虚拟监听器即可。 RS 不会在没有监听器的情况下执行请求。

关于java - RoboSpice 和 ORMLite - 访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892020/

相关文章:

java - 将多行 JTextArea 写入 txt 文件

java - 管理对 Apache Derby 数据库的并发访问

android - 显示来自 Android 应用的配对通知

java - AngularJs - $http.get 请求,响应为 -1 状态代码

Jquery ajax休息服务调用: Typo Error Access is denied

Java泛型、类型删除和泛型成员的类型

java - JFace 数据绑定(bind)不支持默认方法

java - 在 Android 的 Java 中将变量从 Activity 传递到类

Android Studio Section Pager fragment 适配器

api - 如何将分页添加到REST API?