Reputation:
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)")
Upvotes: 1
Views: 287
Reputation: 106
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)")
}
Upvotes: 1