1

I have list of items in CloudKit. I'm getting the data just fine, but would like to sort the tableview items by date.

Specifically, I would like to only show the items that's date are equal to or greater than today's date (now).

TableViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let now = NSDate()

    let container = CKContainer.defaultContainer()
    let publicData = container.publicCloudDatabase

    let query = CKQuery(recordType: "Play", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
    publicData.performQuery(query, inZoneWithID: nil) { results, error in
        if error == nil { // There is no error
            for play in results! {
                let newPlay = Play()

                newPlay.color = play["Color"] as! String
                newPlay.datetime = playoffs["DateTime"] as! AnyObject

                self.objects.append(newPlay)

                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.tableView.reloadData()
            })
            }
        }
        else {
            print(error)
        }
    }
}

Any help would be really appreciated, thanks!

EDIT

Added: let predicate = NSPredicate(format: "DateTime > %@", now)

Replaced: let query = CKQuery(recordType: "Play", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)) with let query = CKQuery(recordType: "Play", predicate: predicate)

Does that make sense?

EDIT 2

This is what it looks like now:

    let sort = NSSortDescriptor(key: "DateTime", ascending: true)

    let query = CKQuery(recordType: "Play", predicate: predicate)
    query.sortDescriptors = [sort]
    publicData.performQuery(query, inZoneWithID: nil) { results, error in

Does that look right?

SRMR
  • 3,064
  • 6
  • 31
  • 59
  • 1
    Change the predicate to return just the desired records. – rmaddy Sep 19 '15 at 21:12
  • 1
    See: http://stackoverflow.com/questions/26521999/how-to-query-by-creationdate-in-cloudkit – Tillson Sep 19 '15 at 21:15
  • @rmaddy does what I updated in my EDIT make sense to you? It looks like it works for me, but I don't know if I'm doing it wrong and its going to break later or something. Thanks! – SRMR Sep 19 '15 at 23:38
  • @Tillson thanks! I think that helped me, does what I edited look right to you? – SRMR Sep 19 '15 at 23:38
  • @rmaddy it is removing objects that aren't greater than todays date; but it's not sorting the objects greater than todays date, by their dates. That's the part I'm stuck on. – SRMR Sep 20 '15 at 00:19
  • @Tillson have any links for how to sort the objects greater than todays date, by their dates? I've got the part where it only shows objects greater than todays date so far – SRMR Sep 20 '15 at 00:20
  • Sort the array by date after you have all of the records. – rmaddy Sep 20 '15 at 00:32
  • @rmaddy Does what I put in EDIT2 look right, or do I need to do the sorting later, like after `self.objects.append(newPlay)` or something? – SRMR Sep 20 '15 at 00:38
  • You need to do the sort and the table reload after the `for` loop is done. – rmaddy Sep 20 '15 at 00:42
  • So what I put up there is going to get me into trouble then? Because it worked, but I obviously don't know what weird bugs lurk around the corner with how/where I put it. I'm assuming the code for sorting where you are saying is a different one liner, do you know what it would be? And then once I've got that sort line, I need to make sure that the sorting and reload is done outside of the for loop brackets? – SRMR Sep 20 '15 at 01:02

0 Answers0