Last active
November 2, 2016 21:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example uses
Groovy
,parse4j
, and mixins.