Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@abdielou 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
You can’t perform that action at this time.