Skip to content

Instantly share code, notes, and snippets.

@abdielou
Last active November 2, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdielou/9425743ab57b05e1222b9b44b27a23ec to your computer and use it in GitHub Desktop.
Save abdielou/9425743ab57b05e1222b9b44b27a23ec to your computer and use it in GitHub Desktop.
Example on how to find more than 1k items with a ParseQuery. The unavoidable hard limit is 10k items as defined by Parse.
/**
* Created by abdielou on 11/2/16.
*/
class FindAllExtension{
public static def findAll(ParseQuery query){
def allItems = []
def limit = 1000
def page = 0
def currSize = 0
def count = query.count()
if(count > 10000)
throw new Exception("Cannot do skips larger than 10000. Filter further.")
Looper.loop {
def currPageResults = findPaged(query,limit,page++)
currSize = currPageResults.size()
allItems.addAll(currPageResults)
} until { currSize < limit && count < (page * limit)}
allItems
}
private static def findPaged(query,limit,page){
query.limit(limit)
query.skip(page * limit)
query.find() ?: []
}
private static class Looper {
private Closure code
static Looper loop( Closure code ) {
new Looper(code:code)
}
void until( Closure test ) {
code()
while (!test()) {
code()
}
}
}
}
// Mixin
ParseQuery.mixin(FindAllExtension)
// Example Query
def exampleQuery = ParseQuery.getQuery("TestObject")
exampleQuery.whereExists("createdAt") // add your filters
println exampleQuery.findAll().size()
@abdielou
Copy link
Author

abdielou commented Nov 2, 2016

This example uses Groovy, parse4j, and mixins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment