如何使用RoboSpice连接待处理的请求(启动>主要活动)?

Gui*_*ara 5 android ormlite robospice

我对RoboSpice(高级用法)的问题很少.

注意:我使用RoboSpice OrmLite.

我的Android应用程序由两个主要活动组成:第一个是SplashActivity(启动时开始),第二个是MainActivity(在启动屏幕后推出5秒).我在启动时执行了2个请求:

SplashActivity

public class SplashActivity extends BaseActivity {

    private fooRequest fooRequest;
    private barRequest barRequest;
    private exampleRequest exampleRequest;
    private NewsRequest newsRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        fooRequest = new fooRequest();
        barRequest = new barRequest();
        ...
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // Launch MainActivity...
            }
        }, 5000);
    }

    @Override
    protected void onStart() {
        super.onStart();

        getSpiceManager().execute(fooRequest, new Integer(0), DurationInMillis.ONE_DAY, new fooRequestListener());
        getSpiceManager().execute(barRequest, new Integer(1), DurationInMillis.ONE_DAY, new barRequestListener());
    }

    public final class fooRequestListener implements RequestListener<Foo> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Foo request failure");
        }

        @Override
        public void onRequestSuccess(final Foo result) {
        }
    }

    public final class barRequestListener implements RequestListener<Bar> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Bar request failure");
        }

        @Override
        public void onRequestSuccess(final Bar result) {
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

问题

我的应用程序逻辑并不可靠:我们无法确定在启动MainAcitivty时请求是否已完成.在MainActivity上,我使用OrmLite查询我的数据库以获取一些数据并显示它们.因此,如果在SplashActivity上开始的请求没有完成,我的视图就不显示任何内容.

问题

1)我认为我需要为待处理的请求添加一个监听器(如果存在这样的请求).在RoboSpice Wiki上,它说要使用spiceManager.addListenerToPendingRequest.尽管我进行了测试,但还是没有成功.也许我做错了什么?你能给我一些代码示例吗? 已解决(见下文)

2)目前,用户在到达主屏幕之前仍在等待5秒(定时器启动).如何检查数据是否在缓存中?有spiceManager.getDataFromCache()(它考虑到期时间?).

3)失败请求的最佳重试策略是什么(a.在第一次启动时,如果不再创建数据库; b.如果数据库存在但数据已过期)?


编辑

问题#1已解决(我在原始代码中犯了一个错误) - #2和#3仍然相关.这是做什么(如果它可以帮助某人......):

public class MainActivity extends BaseActivity {

    private SpiceManager spiceManager = new SpiceManager(CalendarSpiceService.class);
    ...
    @Override
    protected void onStart() {
        super.onStart();

        spiceManager.start(this);
        spiceManager.addListenerIfPending(Foo.class, new Integer(0), new fooRequestListener());
        spiceManager.addListenerIfPending(Bar.class, new Integer(2), new newsRequestListener());
        spiceManager.getFromCache(Foo.class, new Integer(0), DurationInMillis.ONE_DAY, new fooRequestListener());
        spiceManager.getFromCache(Bar.class, new Integer(2), DurationInMillis.ONE_DAY, new newsRequestListener());
    }
    ...
    public final class fooRequestListener implements RequestListener<Foo> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Foo request failure");
        }

        @Override
        public void onRequestSuccess(final Foo result) {
            String test = result.getResult().iterator().next().getTitle();
            Log.w("MyApp", "RoboSpice - Foo request OK ! "+test);
        }
    }

    public final class barRequestListener implements RequestListener<Bar> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Bar request failure");
        }

        @Override
        public void onRequestSuccess(final Bar result) {
            Log.w("MyApp", "RoboSpice - Bar request OK ! "+test);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

但我不明白spiceManager.getFromCache这里的目的......

谢谢!

Sni*_*las 2

我会让启动画面持续更长时间。看起来它的目的确实很模糊:显示徽标或其他东西,或者执行请求并显示“请等待消息”。

在第一种情况下,不执行任何请求就可以了。第二次,等他们回来就好了。

我真的更喜欢那些如果需要启动画面就能快速启动的应用程序。启动画面持续时间应少于 1 秒。之后的第一个屏幕应该处理数据查询并请等待消息。

但回答你的问题:

2)是的,SpiceManager缓存方法使用缓存过期的方式与executeRequest相同。您可以检查缓存中是否有某些内容,但您必须通过指定数据的过期限制来指定“有效”数据的含义。

3)我没有看到重试政策和您的整体问题的链接。默认情况下,RoboSpice 有一个重试策略,对失败的请求重试 3 次。如果您无法从缓存中获取数据,则意味着其中没有任何数据。如果调用侦听器的失败挂钩,则网络请求失败。两者可以是同一个侦听器,并且您的应用程序应该有一个通用机制来 1) 显示出现问题,2) 如果需要一段时间后重新启动请求(可能是刷新按钮/项目菜单)。

我希望我有所帮助,但不太确定。