0

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: enter image description here

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

Roduck Nickes
  • 1,021
  • 2
  • 15
  • 41

2 Answers2

0

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.

Tob
  • 985
  • 1
  • 10
  • 26
0

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

Community
  • 1
  • 1
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • I changed the objectId to the one I have, but it doesn't work.. And the `print("Done!")` does not get executed. – Roduck Nickes May 30 '16 at 23:18
  • What doesn't work? Try printing postsFoundAfterThePost and thePost. Also, are you getting an error? – Pranav Wadhwa May 30 '16 at 23:58
  • Did you try printing the things? Let me know what you get. – Pranav Wadhwa May 31 '16 at 19:01
  • Where exactly should I set the print functions? – Roduck Nickes May 31 '16 at 19:32
  • I tried adding print with numbers 1-10 in the code, to see what gets printed, and it ONLY printed the number "1". Here is the code: http://pastebin.com/nTn1SLBh – Roduck Nickes May 31 '16 at 22:31
  • Did not fully work.. It took the whole class, not only the rows after `woaVSFn89t` but everything.. – Roduck Nickes May 31 '16 at 23:29
  • Try printing thePostTime along withe the rest of your posts' times – Pranav Wadhwa May 31 '16 at 23:42
  • Tried print it right above the `done` print, and all I got was `2016-05-26 12:57:02 +0000`. – Roduck Nickes May 31 '16 at 23:52
  • What about the other posts in the loops? What did you get when you printed their created at? – Pranav Wadhwa Jun 01 '16 at 00:57
  • I tried to print `self.createdAt` above the done print, and got this output: `[2016-05-28 21:50:34 +0000, 2016-05-26 18:49:36 +0000, 2016-05-26 16:54:39 +0000, 2016-05-26 12:57:02 +0000, 2016-05-25 23:16:20 +0000, 2016-05-25 23:14:28 +0000, 2016-05-25 23:04:05 +0000, 2016-05-25 23:03:55 +0000, 2016-05-24 23:35:54 +0000, 2016-05-24 23:29:04 +0000, 2016-05-24 08:51:22 +0000, 2016-05-24 08:49:04 +0000, 2016-05-24 08:46:34 +0000, 2016-05-24 08:45:35 +0000, 2016-05-24 08:44:38 +0000, 2016-05-24 08:43:48 +0000, 2016-05-24 08:42:58 +0000, 2016-05-24 08:42:01 +0000` ++ some more, text was to long. – Roduck Nickes Jun 01 '16 at 09:16
  • I made an edit on the line that compares the dates. Are you using the link that I put for comparing for dates? – Pranav Wadhwa Jun 01 '16 at 11:38
  • It works now! OMG :D Thank you so much! I am also wondering to how only query items added within 24 hours(like snapchat has 24 hours on story). I have to ask a new question for that, or? – Roduck Nickes Jun 01 '16 at 12:01
  • I suggest taking today's date, and subtracting 24 hours. Then do the same date comparison as done here. Also, if my answer helped you, could you please accept it? Thanks – Pranav Wadhwa Jun 01 '16 at 12:49
  • Accepted myfriend! How would i subtract todays date to 24 hours? If i am using my original code to query the whole parse class? – Roduck Nickes Jun 01 '16 at 19:20
  • http://stackoverflow.com/a/5067807 Use obectivec2swift.com to convert the code, but it is pretty self explanatory – Pranav Wadhwa Jun 01 '16 at 19:23
  • So it would be something like this, or?: `query.whereKey(date24HoursAgo, greaterThan: dateToday)` – Roduck Nickes Jun 01 '16 at 19:42
  • Wouldn't it be less than? Your code right now will check if 24 hours ago will be greater than your object's created at. I think it should be less than, but you can try both and see which one works. – Pranav Wadhwa Jun 01 '16 at 21:28