biggreentree
biggreentree

Reputation: 1943

Can't get objects in the order they where created via Parse.com with createdAt - Swift 1.2 - Xcode 6.4

My problem is that messages are not showed in the right order, and on my device/simulator in the timeline tableView I find showed my actual (on Mac/device) date and time in each cell, but I'd like to get messages in the order they where created by values in the "createdat" column on the server.

Don't know how orderByAscending function should be used, so I'm not using it at the moment.

As you can see, dates do not match, and messages are in wrong order, as never reversed them

enter image description here enter image description here

Some info on the app:

From a prepareForSegue in ChannelsTVC, I pick a keyword from an array, and "send" it to MessagesTimelineTVC via filterToParse variable, in order to select only keyword-related messages in "Messages" class.

In the PFQuery //MARK: Retrive Date from Parse 1/3 I ask both for text messages and they date separately, put them in two different arrays, reverse them then finally show them in the tableView.

Thanks in advance.

In MessagesTimelineTVC cellForRowAtIndexPath I have:

//MARK: Retrive Date from Parse 3/3
        var dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
        var useDate = dateFormatter.stringFromDate(self.messageDateArray[indexPath.row])
        cell.dateMessageLabel.text = useDate

The function I retrive data via PFQuery is in viewDidAppear in MessagesTimelineTVC:

func loadData() {



timelineData.removeAll(keepCapacity: true)          //erase previus contents
println("timelineData cleared")


//MARK: Starts the PFQuery
var findTimeLineDataQuery = PFQuery(className: "Messages")

findTimeLineDataQuery.whereKey("channel", containsString: FilterToParse) //MARK: - filter for Parse - should filter parse query

findTimeLineDataQuery.findObjectsInBackgroundWithBlock({
    (objects : [AnyObject]?, error : NSError?) -> Void in

    if error == nil {
        for singleObject in objects! {
            if let stringData = singleObject["message"] as? String {
                self.timelineData.append(stringData)

                //MARK: Retrive Date from Parse 1/3
                if let messageDate = singleObject.createdAt {
                    self.messageDateArray.append(messageDate!)
                    println("retrived date from Parse is: *\(messageDate!)*")
                }
            }
        }

        let reversedArray : Array = self.timelineData.reverse()  //MARK: Always Remember! (reverse array order)
        self.timelineData = reversedArray as Array

        //MARK: Retrive Date from Parse 2/3
        let reversedMessageDateArray : Array = self.messageDateArray.reverse()  //MARK: Always Remember! (reverse array order)
        self.messageDateArray = reversedMessageDateArray as Array


        self.tableView.reloadData()
    }
})
}

Upvotes: 0

Views: 517

Answers (1)

Antoine
Antoine

Reputation: 1149

You should use orderByAscending or orderByDescending within the query variable such as :

var query = PFQuery(className: "name")
query.orderByAscending("createdAt")
//You can use the functions like that

In your case :

findTimeLineDataQuery.orderByAscending("createdAt")

It is the easiest way.

Upvotes: 1

Related Questions