Parse.com PFQuery order by time (Swift)
I think the problem is just that "createdAt"
field starts with a lowercase 'C', not an uppercase, so you should try:
query.orderByDescending("createdAt")
Parse Query in order of creation in Swift
You can also use PFQueryTableViewController class to manage objects from table, override queryForTable
method by returning PFQuery.
PFQuery by date
An NSDate object and the Parse.com Date field both have an exact time that includes hours and minutes. This means that if you are looking for an appointment that takes place on a certain day, you are actually searching a date range: the range from 12:00am until 11:59pm of that particular day.
If you create an NSDate with no explicit hours and minutes, Parse doesn't know you mean "all dates on that day" - it interprets it by populating hours and minutes and searching for an exact time. You get around this with the date range search.
Here is an example of how to do this from one of my apps, modified for your purposes:
//Create two dates for this morning at 12:00am and tonight at 11:59pm
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:1];
NSDate *morningStart = [calendar dateFromComponents:components];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
NSDate *tonightEnd = [calendar dateFromComponents:components];
PFQuery *query = [PFQuery queryWithClassName:@"Booking"];
[query whereKey:@"nextAppointment" greaterThan:morningStart];
[query whereKey:@"nextAppointment" lessThan:tonightEnd];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//Etc...
}
}];
swift parse query not returning objects in ascending order
What you need is a nested query. The array you are trying to get is an of posts. So your query should be on the post table. (Also, your table should be called post
not posts
. Table names should always be singular. Each post is called a post
not a posts
.
let postsQuery = PFQuery(className: "post")
postsQuery.orderByDescending("updatedAt")
let usersQuery = PFQuery(className: "following")
usersQuery.whereKey("follower", equalTo:PFUser.currentUser()!.objectId!)
// this might need to change depending on what the followed user is called in the `followings` table.
postsQuery.whereKey("userId", matchesKey:"followed", inQuery:usersQuery)
getPostsQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for post in objects {
self.dates.append(post.createdAt as NSDate!)
self.messages.append(post["message"] as! String)
}
print(self.dates)
self.myTableView.reloadData()
})
Also, you are saving dates and messages in separate arrays. Create a struct or something that has a date and string and have a single array of Post
objects.
Swift : Parse, query date field based on nsdate()
Your problem is that NSDate
is not just a date, it's an exact moment in time.
And you're very likely to have exactly no records from this precise date and moment in time.
What you should be doing is something like:
let now = NSDate()
let cal = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let midnightOfToday = cal.startOfDayForDate(now)
var query = PFQuery(className:"userBids")
query.whereKey("date", greaterThanOrEqualTo: midnightOfToday)
The above solution only works for iOS 8 & newer (and I found it here). Click on that link if you want to get something that's iOS 7 compatible.
How to sort NSDate in descending order from Parse in ios swift
Add this to your query.
[query addDescendingOrder:@"EventSTDTime"];
Order of data retrieved from parse
PFQuery has function for Sorting you can sort using createdAt
or updatedAt
key in both order:
userQuery.orderByAscending("createdAt")
Or:
userQuery.orderByDescending("createdAt")
Related Topics
Swift: Implementing Protocol Initializer in a Class
Swift Tableview How to Select All Rows
How to Give Dynamic Height to Uitableview
@Published Property Not Triggering Anything
How to Convert [Any] to Nsarray
Nsurlsession Datataskwithrequest Not Being Called
How to Deal with Commas When Writing Objects to CSV in Swift
(Cross-)Compiling Swift for Raspberry Pi
What Does the Underscore in a Function Declaration Do
Swift 2: Multiline Mkpointannotation
Hiding Dividers in Nssplitview
Ordering Firebase Posts Chronologically Swift
Xcode Swift 3: Timer and Segue View Controller Error
Loading Uiviewcontroller from Instantiateviewcontrollerwithidentifier - Nil Outlets