1

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I think it's possible, you'd make a whole new query every time and you'd increase the skip of the inner query. Though i've never needed to do this and my knowledge is only theoritical, i think it's the exact same with or without the inner query
Thanks for your answer. It's not simple as you think. If I don't use inner query, then you are right. The problem is in inner query. I should get first post object after getting all users. But current code is getting first post object among 1000 user posts at most. After all I don't get first post object in all post objects. This is a trouble which I have.
@KarlBedera your only getting 1 post because you set your limit to return 1...what else do you need to fix it?
I want to get first one all over post objects.
I want to get first one all over post objects. Suppose that app have 1000+ users which their deactive value is false and 1500th user's post object is largest. Then above code doesn't return largest object because only 1000 user's post objects are resulted by query.
1

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.

Comments

0

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.

2 Comments

If user has lots of posts, then "active" field of his all posts should be set to "false" at once? Inner query is used a lot including push notification. Isn't there any other way?
you can try a for loop .. you count how many photos you must fetch, once done, you set the cycle by dividing the number by 1000 .. is cumbersome but if you can not change the structure there are no alternatives.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.