我在 Kinvey 网站上遵循了这个指南 ,现在在我的 MainActivity 中有这样的东西:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Client mKinveyClient = new Client.Builder(this.getApplicationContext()).build();
final AsyncAppData<MyEntity> ad = mKinveyClient.appData("myCollection", MyEntity.class);
ad.setCache(new InMemoryLRUCache(), CachePolicy.CACHEFIRST);
ad.setOffline(OfflinePolicy.LOCAL_FIRST, new SqlLiteOfflineStore<MyEntity>(this));
MyEntity event = new MyEntity();
event.setName("Launch Party");
event.setAddress("Kinvey HQ");
ad.save(event, new KinveyClientCallback<MyEntity>() {
@Override
public void onFailure(Throwable e) {
Log.e("TAG", "failed to save event data", e);
}
@Override
public void onSuccess(MyEntity r) {
Log.d("TAG", "saved data for entity "+ r.getName());
ad.get(new KinveyListCallback<MyEntity>() {
@Override
public void onSuccess(MyEntity[] result) {
Log.v("TAG", "received "+ result.length + " events");
}
@Override
public void onFailure(Throwable error) {
Log.e("TAG", "failed to fetch all", error);
}
});
}
});
}
现在,如果手机在线并且我确实启动了几次应用程序,我会得到以下输出:
V/TAG﹕ received 0 events
V/TAG﹕ received 1 events
V/TAG﹕ received 2 events
V/TAG﹕ received 3 events
...
但是如果我激活飞行模式来模拟离线使用:
V/TAG﹕ received 3 events
V/TAG﹕ received 3 events
V/TAG﹕ received 3 events
V/TAG﹕ received 3 events
...
如您所见,AppData 在离线时不会获取最近保存的实体。我是不是搞错了,或者这种缓存是不可能的?