1

I want remove the limit to get all data from parse with this code below. but is not working, I didn't find a simple way to do it with swift, I found a post similar to that How to Fetch all data in a table using PFQuery in iOS? but with objec

can someone help me I'm new in swift

 var allObjects: [AnyObject] = NSMutableArray() as [AnyObject]
        let limit: Int = 1000
        var skip: Int = 0
        let posts1 = PFQuery(className:"Post")
      
        if let user = PFUser.currentUser(){
        let radius = 100000000000000000000000000000000000.0
        posts1.limit = limit
        posts1.whereKey("createdBy", equalTo: user)
        posts1.whereKey("location", nearGeoPoint: currentLoc, withinKilometers: radius)
        posts1.skip = skip
        posts1.findObjectsInBackgroundWithBlock({(objects: [AnyObject]?, error: NSError?) in
            
            if (error == nil) {
               allObjects.append(objects!)
                
                
            //allObjects.addObjectsFromArray(objects)
            if objects!.count == limit {
                skip += limit
                posts1.skip = skip
                posts1.findObjectsInBackgroundWithBlock({(objects: [AnyObject]?, error: NSError?) in
                    
                    if (error == nil) {
                        allObjects.append(objects!)
                       // allObjects.addObjectsFromArray(objects)
                  
               
                    }
                })
            }
        }
        else {
            print("Error: %@ %@", error, error!.userInfo)
                }
            
            })
        }
                 print("\(allObjects)")

1
  • 1
    If you need to query for more objects than the limit, your data model is suboptimal. You should always plan your data model based on your queries when designing mobile apps. It would be very slow and inefficient for a mobile device to fetch this many objects. There are various ways to handle this, depending on what you want do do with the data you fetch. If you describe your use case, it would be easier to suggest solutions. In any case, it is highly unlikely that getting all objects is the right solution. Commented Nov 23, 2015 at 10:45

1 Answer 1

1

One way of doing this is to use the recursion method, place your above code inside a function, and call this function whenever objects!.count == limit

eg.

var allObjects: [AnyObject] = NSMutableArray() as [AnyObject]
var querySkip = 0
let limit: Int = 1000
func getDataFromParse() {
    let posts1 = PFQuery(className:"Post")

        if let user = PFUser.currentUser(){
        let radius = 100000000000000000000000000000000000.0
        posts1.limit = limit
        posts1.whereKey("createdBy", equalTo: user)
        posts1.whereKey("location", nearGeoPoint: currentLoc, withinKilometers: radius)
        posts1.skip = self.querySkip
        posts1.findObjectsInBackgroundWithBlock({(objects: [AnyObject]?, error: NSError?) -> Void in

            if (error == nil) {
               allObjects.append(objects!)


                //allObjects.addObjectsFromArray(objects)
                if objects!.count == limit {
                   self.querySkip += self.limit
                   self.getDataFromParse()
                } else {
                   // All Data are loaded here
                }
            } else {
                 print("Error: %@ %@", error, error!.userInfo)
            }

        })
        }
                 print("\(allObjects)")

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

Comments

Your Answer

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