使用PFQuery swift

时间:2015-11-22 05:52:55

标签: swift parse-platform

我想删除限制以使用以下代码解析所有数据。但是没有用,我找不到用swift做一个简单的方法,我发现了一个类似于How to Fetch all data in a table using PFQuery in iOS?的帖子,但是有了objec

有人能帮助我,我是一个快速的新人

 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 个答案:

答案 0 :(得分:1)

这样做的一种方法是使用递归方法,将上面的代码放在函数中,并在对象时调用此函数!.count == limit

例如

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)")

}