Reputation: 435
I query messages stored on parse.com
let query = PFQuery(className: "Messages")
query.whereKey("sender", equalTo: self.sender)
query.whereKey("receiver", equalTo: self.receiver)
query.orderByAscending("createdAt")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
var messages = [Int: [AnyObject]]()
if let objects = objects as? [PFObject] {
var i: Int = 0
for object in objects {
var temp = [AnyObject]()
temp.append(object["sender"]!)
temp.append(object["receiver"]!)
temp.append(object.createdAt!)
temp.append(object["message"]!)
messages[i] = temp
i = i+1
}
print("Messages: \(messages)")
}
} else {
print("Error!")
}
}
The result looks like
Messages: [2: [Phil, Eff, 2015-08-27 15:05:04 +0000, three], 0: [Phil, Eff, 2015-08-27 15:04:57 +0000, one], 1: [Phil, Eff, 2015-08-27 15:05:00 +0000, two]]
It seems like query.orderByAscending("createdAt")
is doing nothing?
Upvotes: 0
Views: 533
Reputation: 10612
createdAt
is doing what it's intended to do. It's how your adding the data to you object.
You need to forego the int var and simply use :
messages = temp
, because you are running a loop for every object in objects. Each message can only equal it's counterpart, no reason to specify an index for it
To add your message to an array simply do the following:
var messagesArray:NSMutableArray = []
..findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let object = objects as ? [PFObject] {
for object in objects {
...
messages = temp
self.messagesArray.addObject(messages)
...
Upvotes: 1