Comments (2)
I would appreciate this functionality too.
Workaround: Create copies of Reservoir.java and SimpleDiskCache.java in your own project and add the mentioned methods. (You don't need to modify SimpleDiskCache for this, but that class is not public and your copy of Reservoir needs it).
I've already done this, but I could create a PR for this.
/**
* Get an object from Reservoir with the given key. This a blocking IO operation.
*
* @param key the key string.
* @param type the Type of the expected return object.
* @return the object of the given type if it exists, otherwise <code>null</code>.
* @throws java.lang.NullPointerException if key does not exist or java.lang.IOException if reading from file fails.
*/
public static <T> T getByType(String key, Type type) throws Exception {
String json = cache.getString(key).getString();
return new Gson().fromJson(json, type);
}
/**
* Get an object from Reservoir with the given key asynchronously.
*
* @param key the key string.
* @param type the Type of the expected return object.
* @param callback a callback of type {@link com.anupcowkur.reservoir.ReservoirGetCallback}
* which is called upon completion.
*/
public static <T> void getAsyncByType(String key, Type type, ReservoirGetCallback<T> callback) {
new GetByKeyAndTypeTask<>(key, type, callback).execute();
}
/**
* AsyncTask to perform get operation in a background thread.
*/
private static class GetByKeyAndTypeTask<T> extends AsyncTask<Void, Void, T> {
private final String key;
private final ReservoirGetCallback<T> callback;
private final Type type;
private Exception e;
private GetByKeyAndTypeTask(String key, Type type, ReservoirGetCallback<T> callback) {
this.key = key;
this.callback = callback;
this.type = type;
this.e = null;
}
@Override
protected T doInBackground(Void... params) {
try {
String json = cache.getString(key).getString();
T value = new Gson().fromJson(json, type);
if (value == null)
throw new NullPointerException();
return value;
} catch (Exception e) {
this.e = e;
return null;
}
}
@Override
protected void onPostExecute(T object) {
if (callback != null) {
if (e == null) {
callback.onSuccess(object);
} else {
callback.onFailure(e);
}
}
}
}
(I haven't created versions for RxJava)
from reservoir.
@ciskeboekelo Yes, I guess that will work too, but as you said, is far from ideal.
Thanks for sharing your solution!
from reservoir.
Related Issues (20)
- RxAndroid as a provided dependency HOT 1
- Failed to invoke protected java.text.DateFormat() with no args HOT 1
- Is there any way to set expiry date for a cached object? HOT 1
- cannot access Observable class file for rx.Observable not found HOT 10
- this project support multiprocess operation ? HOT 1
- Proguard Rules HOT 2
- GSON Error HOT 1
- A/libc: Fatal signal 11 (SIGSEGV) HOT 2
- Declares multiple JSON fields named id HOT 3
- bytesUsed not working HOT 1
- Add more specific exceptions in README HOT 1
- null object reference! HOT 3
- Any chance for iOS ? HOT 1
- Invalidate Cache when app updates HOT 1
- ability to cancel asynctask HOT 1
- Would be support of RxJava2? HOT 2
- rx.java seems not optionable - `Failed resolution of rx/Observable$onSubscribe` error HOT 1
- Can we put multiple keys into cache? HOT 3
- Replace RxJava 1 with RxJava 2 HOT 4
- What can be the causes for faliure for storing HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reservoir.