Mathew Antony
Mathew Antony

Reputation: 173

Fetching > 1000 entries from a Parse class

I'm trying to fetch information from a class that contains 2500 entries. This code worked when I tried for just a 1000 entries.

However, when I put it inside a for loop, it's throwing the following error: 'NSInternalInconsistencyException', reason: 'This query has an outstanding network connection. You have to wait until it's done.'

Here's the code:

func loadDataFromParse(){
    var classLimit = 2
    println("Loading Parse data")
    var query = PFQuery(className: "StopList")
    query.limit = 1000

    for (var j = 0; j < classLimit+1; j++)
    {
        query.skip = 1000*j

        query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
            if(error == nil){
                if let stopObjects = objects as? [PFObject] {
                    for stop in stopObjects {
                        // Code goes here
                    }
                }}
            else{
                println("Error in retrieving \(error)")
            }

        })

    }


}

Any ideas on what's going on?

Upvotes: 2

Views: 422

Answers (1)

Mathew Antony
Mathew Antony

Reputation: 173

Figured it out! Needed to reinitialize the query variable every time the loop iterates:

func loadDataFromParse(){
    var i = 1
    var limit = 2
    println("Loading Parse data")

    for (var j = 0; j < limit+1; j++)
    {
        var query = PFQuery(className: "StopList")
        query.limit = 1000
        query.skip = 1000*j
        query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
            if(error == nil){
                if let stopObjects = objects as? [PFObject] {
                    for stop in stopObjects {
                        println(i)
                        i += 1
                    }
                }}
            else{
                println("Error in retrieving \(error)")
            }       
        })     
    }
}

Upvotes: 4

Related Questions