当用户点击主页按钮时,RoboSpice请求结果 [英] RoboSpice requests results when user taps home button

查看:100
本文介绍了当用户点击主页按钮时,RoboSpice请求结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用带有okhttp的Robospice 1.4.12.一切正常,除了当用户点击主页"按钮然后返回到活动时从请求中获取结果.

示例:我们触发一个请求,然后按主页按钮,然后等待请求完成,当我们返回到活动时,监听器将永远不会收到通知.如果我们在请求完成之前回来,一切正常.

方向更改工作正常.我们正在使用LruCacheObjectPersister进行内存缓存.

现在输入一些代码:

我们的服务:

public class MyService extends OkHttpSpiceService {

    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        int cacheSize = 1 * 1024 * 1024; // 1MiB
        cacheManager.addPersister(new LruCacheObjectPersister<LoginModel>(LoginModel.class,
                new LruCache<Object, CacheItem<LoginModel>>(cacheSize)));
        return cacheManager;
    }

}

片段中的onStart:

@Override
    public void onStart() {
        super.onStart();
            //spice manager gets started in base fragment
        spiceManager.addListenerIfPending(LoginModel.class, REQUEST_KEY, new LoginRequestListener());
    }

请求执行:

spiceManager.execute(new LoginRequest(), REQUEST_KEY, DurationInMillis.ONE_DAY,
                        new LoginRequestListener());

我们错过了什么吗?

解决方案

RS的工作方式.您必须了解,在Android上,您永远无法确保按下主屏幕之前的活动与返回之后的活动是同一实例.同时,Android可以垃圾收集您的活动.是的,这发生了!!

因此,RS所做的是,它在不同的上下文中执行查询,以确保启动请求上下文的生存期足以执行请求.活动不是这种情况.

话虽如此,如果您想重新连接到待处理的请求,请查看spice manager方法.您将看到一个addListenerIfPending something方法.这就是这样做的方式. RS还让您检查是否存在这样的挂起请求.您还可以检查缓存中是否已经包含您的数据,等等,等等.

We are using Robospice 1.4.12 with okhttp. Everything is working fine except getting the results from a request when user taps home button and then comes back to the activity.

Example: we fire a request and then press home button and we wait for request to finish, when we come back to the activity the listener never gets notified. If we come back before the request finishes everything works ok.

Orientation changes are working fine. We are using LruCacheObjectPersister for in memory cache.

Now some code:

Our service:

public class MyService extends OkHttpSpiceService {

    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        int cacheSize = 1 * 1024 * 1024; // 1MiB
        cacheManager.addPersister(new LruCacheObjectPersister<LoginModel>(LoginModel.class,
                new LruCache<Object, CacheItem<LoginModel>>(cacheSize)));
        return cacheManager;
    }

}

Our onStart in the fragment:

@Override
    public void onStart() {
        super.onStart();
            //spice manager gets started in base fragment
        spiceManager.addListenerIfPending(LoginModel.class, REQUEST_KEY, new LoginRequestListener());
    }

Request Execution:

spiceManager.execute(new LoginRequest(), REQUEST_KEY, DurationInMillis.ONE_DAY,
                        new LoginRequestListener());

Are we missing something?

解决方案

This how RS works. You have to understand that, on Android, you can never get sure that the activity before pressing home and after getting back are the same instance. Meanwhile, android can garbage collect your activity. And yes, this happens !!

So, what RS does is that it executes queries in a different context to get sure that the launching request context will live long enough to execute the request. Which is not the case with an activity.

That being said, if you want to reconnect to a pending request, look a spice manager methods. You will see a addListenerIfPending something method. That's the way to do it. RS also let's you check if such a pending request exists. You can also check if the cache contains your data already, etc, etc.

这篇关于当用户点击主页按钮时,RoboSpice请求结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆