Skip to content

Instantly share code, notes, and snippets.

@Jatapiaro
Last active May 8, 2017 00:29
Show Gist options
  • Save Jatapiaro/36bef653e5bcaf78d455dc23b66680a7 to your computer and use it in GitHub Desktop.
Save Jatapiaro/36bef653e5bcaf78d455dc23b66680a7 to your computer and use it in GitHub Desktop.
public <T> List<T> list(Class c){
final String type = this.getClassName(c);
UUID idOne = UUID.randomUUID();
List<T> l = new ArrayList<T>();
View view = this.dc.getDatabase().getView(type);
view.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if(document.get("type").equals(type)) {
emitter.emit(document.get("_id"), document.get(type));
}
}
}, idOne.toString());
LiveQuery liveQuery = view.createQuery().toLiveQuery();
QueryEnumerator r = null;
try {
r = liveQuery.run();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
Gson gson = new Gson();
for(Iterator<QueryRow> it=r;it.hasNext();){
QueryRow qr = it.next();
Document doc = qr.getDocument();
String jsonString = gson.toJson(doc.getProperties().get(type), Map.class);
T t = (T) gson.fromJson(jsonString, c);
l.add(t);
}
return l;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment