Karl Bedera
Karl Bedera

Reputation: 11

iOS Parse.com inner query doesn't fetch all objects

I am getting trouble with parse.com inner query using. Here is my code.

    PFQuery *innerQuery = [PFUser query]; 
    [innerQuery whereKey:@"deactive" equalTo:[NSNumber numberWithBool:NO]]; 
    PFQuery *query = [PFQuery queryWithClassName:@"Post"]; 
    [query orderByDescending:@"voteCount"]; 
    [query setLimit:1]; 
    [query whereKey:@"user" matchesQuery:innerQuery]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray objects, NSError error) { 

        //some code 
    }]; 

Here is a problem. Inner query max limit count is 1000 as well in Parse.com. In my code, after retrieving 1000 objects in User class and execute query among them. If user count exceeds 1000, then rest other users are out of inner query. so I can't get post object which it's voteCount property is largest.

How can I resolve it? Is it impossible on Parse?

Upvotes: 1

Views: 558

Answers (4)

Septronic
Septronic

Reputation: 1176

I'm wondering, would it help to do a query and set it to Ascending sorting, so that you'd get the earliest results first. Then combine it with the setSkip and setLimit to skip the first however many number of 'False' posts? this is the only alternative I can think of.

Another workaround this would be to set an expiry period for the posts (I'm not sure if whatever you are using this for can accommodate for this), so that if a post has been created or modified however many months / days / years ago, it will be seen as inactive. This way, you can do a query on modified or created at property of the items, and set the criteria for the number of days from the date.

So for example, you query for items with updatedAt property of later than 1st August 2015 if you want to have the items that have been active within the past 6 months.

You can combine them, so that you first get all the posts, then you set the date criteria and search for the active entries.

I hope this helps.

Upvotes: 0

TheMiloNet
TheMiloNet

Reputation: 363

You should change the structure, I think I understand that you want to fetch only post active users. I suggest you add a key to the post called "active". Each time you disable the user, set "false" to the key "active" Then fetch only posts with the key "active" set to true.

Upvotes: 0

Gil Sand
Gil Sand

Reputation: 6040

You need to restart a query inside your completion block.

If you have 1000 results, we assume that there is more to fetch and do another query with a new parameter, the skip. Skip does the exact same as what you know but will skip the X first results.

So you have your limit of 1000, a result of 1000, and a new query to make.

You must give your limit value to your skip value, so you skip exactly the same number (usually just the max, 1000) for your second batch.

You repeat this until your result count is < than 1000, which means there aren't any more objects to fetch.

Setting the skip value is the same as setting the limit.

[query setLimit:1000];
[query setSkip:1000];

or if you like dynamicness

[query setLimit:1000];
[query setSkip:query.limit];  

EDIT : If you don't need all information but only the most important one (which is usually just the latest in date), then you can simply sort your query.

[query orderByAscending:@"createdAt"];   //or descending, or updatedAt, or your own fields

You can also use predicates or more sort descriptors to reduce the size of your request.

Upvotes: 2

Schemetrical
Schemetrical

Reputation: 5536

You can setSkip. So you do the fetch, and for the next 1000, you set the skip to 1000, and same for the next and the next.

Upvotes: 1

Related Questions