【问题标题】:RoboSpice and ORMLite - Access to the dataRoboSpice 和 ORMLite - 访问数据
【发布时间】:2014-01-02 21:15:06
【问题描述】:

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

目前,我设法:

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

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

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

基础活动

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)和其他活动组成。启动画面会在几秒钟后启动 MainActivity(暂时只是一个计时器)。

我想做的是从初始屏幕下载所有数据(RoboSpice 已经将这些数据缓存/保存在 SQLite DB 中),然后从其他活动中访问它。


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

The Wiki page 关于“RoboSpiceDataBaseHelper”对我来说不是很清楚:据说“RoboSpiceDataBaseHelper 用于管理数据库创建和版本管理”,但在下一段中指出某些方法允许“查询和检索数据来自数据库”。在 RoboSpice 示例中,没有关于从其他活动中检索数据的内容。如何发挥 ORMLite 的威力?

我读过来自 Snicolas 的this Google Groups topic

请注意,RoboSpice 缓存是透明的,并且完全封装在 SpiceService 中。应该可以通过对缓存系统的共享引用(由服务和您的活动共享)直接访问缓存系统(在您的情况下为数据库)。此外,在这种情况下,没有什么可以防止并发访问或数据库不一致等副作用。

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

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


问题 2:

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

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


谢谢!

【问题讨论】:

    标签: java android rest ormlite robospice


    【解决方案1】:

    问题一:

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

    最简洁的方法是使用像 RoboGuice 这样的注入框架。一个快速而肮脏的 hack 是为此实例创建一个静态提供程序,甚至更肮脏和更快,使 RoboSpiceDatabaseHelper 成为单例。

    ---更新

    当您使用 RoboGuice 时,您可以:

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

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

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

    @Inject
    RoboSpiceDatabaseHelper databaseHelper;
    

    然后你会得到一个合适的单例,允许你共享数据库访问。

    问题2:

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

    【讨论】:

    • 感谢您的回复。我已经将 RoboGuice 用于“ContentView”和“InjectView”,但我不知道如何使用它来共享“RoboSpiceDatabaseHelper”。对于更脏的黑客:pastebin.com/EjkkHvTu(未测试)。你觉得这样可以吗?
    • 在 Wiki 中,您 (?) 谈到扩展“InDatabaseObjectPersister”(例如,用于持久帐户 POJO 的 InDatabaseAccountPersister)。你有代码实现/示例吗?
    • 您应该为此使用 ORMLite DAO。
    • 我在使用 RoboGuice 注入的所有 Spice 请求中都出现错误(我遵循了您的代码):pastebin.com/xsFUDsLN
    • 我终于设法让它工作了......但只有“肮脏的黑客”(Singleton)。如果我可以使用 RoboGuice 注射,我会很酷。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多