3

I've the parse database name as Timetable, I want to get the data from the column "Intake", but I've a lot of data in it, around 5000 records. I know the maximum query objects that we can get is 1000, but how to get more than 1000 records? The codes that I wrote is only query for 1000

var query = PFQuery(className: "Timetable")
        var limit:NSInteger = 1000
        var skip:NSInteger = 0
        query.limit = limit
        query.skip = skip
        query.orderByAscending("Intake")
        query.findObjectsInBackgroundWithBlock
        {
            (objects:[AnyObject]?, error:NSError?) -> Void in
                if error == nil
                {
                    query.findObjectsInBackgroundWithBlock
                        {
                            (objects:[AnyObject]?, error:NSError?) -> Void in
                            if error == nil
                            {
                                for object in objects! as [AnyObject]
                                {
                                    var intakeCode = object["Intake"] as? String
                                    if !self.pickerString.containsObject(object["Intake"] as! String) {
                                        self.pickerString.addObject(object["Intake"] as! String)
                                    }
                                }
                                self.pvIntakeCode.reloadAllComponents()
                            }
                            else
                            {
                                NSLog("Error: %@ %@", error!, error!.userInfo!)
                            }
                    }
                }
                else
                {
                    NSLog("Error: %@ %@", error!, error!.userInfo!)
                }

I know this problem is existing from stackoverflow, but I really don't understand what it says. (Another question is from here:Parse.com query for 10000 (10K) objects). But I don't understand about it, sorry for asking the question again.

Community
  • 1
  • 1
jefferyleo
  • 630
  • 3
  • 17
  • 34
  • @Dato' Mohammad Nurdin I tried the latest code that you've edited, it is still the same output which only get the first 1000 records from the table there – jefferyleo May 20 '15 at 05:31
  • 1
    Possible duplicate of [Parse.com - retrieve more than 1000 rows](http://stackoverflow.com/questions/17246991/parse-com-retrieve-more-than-1000-rows) – Termininja Jan 28 '16 at 00:04

1 Answers1

1

The limit of 1000 records is there for a reason. You should never need to retrieve that many objects in one query, as this will most likely clog down your app and yield sluggish performance.

I can see two scenarios why you want to retrieve all records:

  1. You want to present all the records to the user
  2. You want to do operations on all records

In scenario 1, what you really want is to page through the results. Since you can only show a few records on the screen at once, you should rather query for i.e. 100 records, and then trigger a new query for the next 100 records as the user is scrolling and nearing the end of the first 100 etc, until the user has scrolled through them all (which would be a very patient user...). You solve this by querying with a limit and a skip:

query.setLimit(100)
query.setSkip(skip)

First run, skip is 0. For every consecutive run, you increase skip with 100.

In scenario 2, you don't want to do this on the client. Enter background jobs! https://parse.com/docs/js/guide#cloud-code-advanced-background-jobs

Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • I've work for the query.limit and query.skip already, But when I do the skip, for example I skip at first is 0, so in the next steps, I will be skip based on my limit, so I will do skip += limit, but when I do this, it only show me the result of after 1000 to 1999 but not 0 to 999 – jefferyleo May 20 '15 at 11:56
  • Then I guess you're not running the first query with skip = 0. Update your question with code for us to see. – Marius Waldal May 25 '15 at 20:01
  • LP, I have an iOS app and I might have a query > 1000. I technically don't need all objects, but I need a random set so they can't just be the first n objects only but rather actually random. I was thinking to query all objects (run multiple queries if its greater than 1000) and then manipulate them as I wish. – Ace Green Dec 23 '15 at 00:01
  • Hi.. @Moonwalkr is right. I think its very poor practice to fetch too many objects at once. For example, what is ou have 1,000,000 users? do you really want to fetch them all at once. You should use sampling. For example, if you need to fetch 1,000 random samples from a population of 1,000,000, one example is to limit to a number X, equal to 100, and set skip to a random number Y, say 20, fetch data, pick out a random fraction Z of the data, and continue until you have your desired 1,000 random samples. – nyxee Jul 31 '17 at 18:19