Parse - Query after a specific row

2019-09-06 06:35发布

问题:

How can I query after a specific row where the objectId is equal to a objectId I have stored?

This is my query code:

func queryStory(){
        let query = PFQuery(className: "myClassStory")
        query.whereKey("isPending", equalTo: false)
        query.limit = 1000
        query.orderByDescending("createdAt")

        query.findObjectsInBackgroundWithBlock { (posts: [PFObject]?, error: NSError?) -> Void in
            if (error == nil){
                // Success fetching objects

                for post in posts! {

                    if let imagefile = post["userFile"] as? PFFile {
                        self.userFile.append(post["userFile"] as! PFFile)
                        self.objID.append(post.objectId!)
                        self.createdAt.append(post.createdAt!)
                    }
                }
                print("Done!")
            }
            else{
                print(error)
            }
        }
    }

This is my Parse database class:

What I want, is to only query the items that was createdAt after the objectId: woaVSFn89t. How can I do this?

回答1:

Try filtering the objects once you have found them:

query.findObjectsInBackgroundWithBlock { (posts: [PFObject]?, error: NSError?) -> Void in
        if (error == nil){
            // Success fetching objects

            var thePostTime = NSDate()

            for post in posts! {
                if post.objectId == "The Object Id You Were Trying To Find" {
                    thePostTime = post.createdAt!
                }
            }

            for post in posts! {

                if post.createdAt!.isGreaterThan(thePostTime) == true {
                    if let imagefile = post["userFile"] as? PFFile {
                        self.userFile.append(post["userFile"] as! PFFile)
                        self.objID.append(post.objectId!)
                        self.createdAt.append(post.createdAt!)
                    }
                }

            }

            print("Done!")
        }
        else{
            print(error)
        }
    }

You will notice that I compared the dates using this: NSDate Comparison using Swift



回答2:

Before the for-loop make a variable:

var havePassedObjectId = false

Then inside the for-loop check if the current post is equal to the object id you want:

if post.objectid == "woaVSFn89t" {
self.userFile.append(post["userFile"] as! PFFile)
 //Continue appending to arrays where needed  
havePassedObjectId = true
} else if havePassedObjectId == true {
   self.userFile.append(post["userFile"] as! PFFile)
   //Continue appending to arrays where needed  
}

This will check if you have already passed the object and append all the objects after.