How do I paginate using swift and firebase?
I am working on an iOS app which has features similar to Instagram. I have successfully done pagination on my profile page but constantly failing to get the right result in my home feed.
Goal - to fetch the posts in chronological order by date. Latest one should come first. Posts are from the people I follow. Fetching 3 first and then another 3 when scrolled down.
Sample firebase real-time database structure.
Users table
users
-uid
-name
-emailID
posts table
posts
-uid
-pid
-title
-date
-pid2
-title
-date
Controller.swift
I get the all the uids of the particular user is following
and then I try to fetch posts from those uids
fileprivate func fetchPosts() {
guard let uid = Auth.auth().currentUser?.uid else { return }
Database.fetchUserWithUID(uid: uid) { (user) in
self.fetchPostsWithUser(user: user)
}
}
fileprivate func fetchPostsWithUser(user: User) {
let uid = user.uid
let ref = Database.database().reference().child("posts").child(uid)
var query = ref.queryOrdered(byChild: "creationDate")
if posts.count > 0 {
let value = posts.last?.creationDate.timeIntervalSince1970
query = query.queryEnding(atValue: value)
}
query.queryLimited(toLast: 3).observeSingleEvent(of: .value, with: { (snapshot) in
guard var allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.reverse()
print("coint is , ", allObjects.count)
if allObjects.count < 2 {
self.isFinishedPaging = true
} else {
self.isFinishedPaging = false
}
if self.posts.count > 0 && allObjects.count > 0 {
allObjects.removeFirst()
}
allObjects.forEach({ (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
var post = Post(user: user, dictionary: dictionary)
post.id = snapshot.key
self.posts.append(post)
})
self.collectionView?.reloadData()
}) { (err) in
print(err)
}
}
extension
extension Database {
static func fetchUserWithUID(uid: String, completion: @escaping (User) -> ()) {
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String: Any] else { return }
let user = User(uid: uid, dictionary: userDictionary)
completion(user)
}) { (err) in
print("Failed to fetch user for posts:", err)
}
}
This code works but weirdly. I don't get the posts in right order. It is always giving me an output of randomly ordered posts.
What should I do?
swift firebase pagination swift4.1
add a comment |
I am working on an iOS app which has features similar to Instagram. I have successfully done pagination on my profile page but constantly failing to get the right result in my home feed.
Goal - to fetch the posts in chronological order by date. Latest one should come first. Posts are from the people I follow. Fetching 3 first and then another 3 when scrolled down.
Sample firebase real-time database structure.
Users table
users
-uid
-name
-emailID
posts table
posts
-uid
-pid
-title
-date
-pid2
-title
-date
Controller.swift
I get the all the uids of the particular user is following
and then I try to fetch posts from those uids
fileprivate func fetchPosts() {
guard let uid = Auth.auth().currentUser?.uid else { return }
Database.fetchUserWithUID(uid: uid) { (user) in
self.fetchPostsWithUser(user: user)
}
}
fileprivate func fetchPostsWithUser(user: User) {
let uid = user.uid
let ref = Database.database().reference().child("posts").child(uid)
var query = ref.queryOrdered(byChild: "creationDate")
if posts.count > 0 {
let value = posts.last?.creationDate.timeIntervalSince1970
query = query.queryEnding(atValue: value)
}
query.queryLimited(toLast: 3).observeSingleEvent(of: .value, with: { (snapshot) in
guard var allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.reverse()
print("coint is , ", allObjects.count)
if allObjects.count < 2 {
self.isFinishedPaging = true
} else {
self.isFinishedPaging = false
}
if self.posts.count > 0 && allObjects.count > 0 {
allObjects.removeFirst()
}
allObjects.forEach({ (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
var post = Post(user: user, dictionary: dictionary)
post.id = snapshot.key
self.posts.append(post)
})
self.collectionView?.reloadData()
}) { (err) in
print(err)
}
}
extension
extension Database {
static func fetchUserWithUID(uid: String, completion: @escaping (User) -> ()) {
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String: Any] else { return }
let user = User(uid: uid, dictionary: userDictionary)
completion(user)
}) { (err) in
print("Failed to fetch user for posts:", err)
}
}
This code works but weirdly. I don't get the posts in right order. It is always giving me an output of randomly ordered posts.
What should I do?
swift firebase pagination swift4.1
I am not seeing anything really wrong with this code, and running it in a test app works consistently and does what it's supposed to in the correct order. However, I don't know how your creationDate string is stored in Firebase. It may also be easier instead of reversing allobjects to build an array, simply clearing the post array, then iterate over the snapshot.children and insert each new post at position 0. The will naturally put the most recent post at the top. Also, you need to ensure every posts node contains a creationDate child as if not, it won't sort properly.
– Jay
Jul 1 '18 at 12:56
add a comment |
I am working on an iOS app which has features similar to Instagram. I have successfully done pagination on my profile page but constantly failing to get the right result in my home feed.
Goal - to fetch the posts in chronological order by date. Latest one should come first. Posts are from the people I follow. Fetching 3 first and then another 3 when scrolled down.
Sample firebase real-time database structure.
Users table
users
-uid
-name
-emailID
posts table
posts
-uid
-pid
-title
-date
-pid2
-title
-date
Controller.swift
I get the all the uids of the particular user is following
and then I try to fetch posts from those uids
fileprivate func fetchPosts() {
guard let uid = Auth.auth().currentUser?.uid else { return }
Database.fetchUserWithUID(uid: uid) { (user) in
self.fetchPostsWithUser(user: user)
}
}
fileprivate func fetchPostsWithUser(user: User) {
let uid = user.uid
let ref = Database.database().reference().child("posts").child(uid)
var query = ref.queryOrdered(byChild: "creationDate")
if posts.count > 0 {
let value = posts.last?.creationDate.timeIntervalSince1970
query = query.queryEnding(atValue: value)
}
query.queryLimited(toLast: 3).observeSingleEvent(of: .value, with: { (snapshot) in
guard var allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.reverse()
print("coint is , ", allObjects.count)
if allObjects.count < 2 {
self.isFinishedPaging = true
} else {
self.isFinishedPaging = false
}
if self.posts.count > 0 && allObjects.count > 0 {
allObjects.removeFirst()
}
allObjects.forEach({ (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
var post = Post(user: user, dictionary: dictionary)
post.id = snapshot.key
self.posts.append(post)
})
self.collectionView?.reloadData()
}) { (err) in
print(err)
}
}
extension
extension Database {
static func fetchUserWithUID(uid: String, completion: @escaping (User) -> ()) {
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String: Any] else { return }
let user = User(uid: uid, dictionary: userDictionary)
completion(user)
}) { (err) in
print("Failed to fetch user for posts:", err)
}
}
This code works but weirdly. I don't get the posts in right order. It is always giving me an output of randomly ordered posts.
What should I do?
swift firebase pagination swift4.1
I am working on an iOS app which has features similar to Instagram. I have successfully done pagination on my profile page but constantly failing to get the right result in my home feed.
Goal - to fetch the posts in chronological order by date. Latest one should come first. Posts are from the people I follow. Fetching 3 first and then another 3 when scrolled down.
Sample firebase real-time database structure.
Users table
users
-uid
-name
-emailID
posts table
posts
-uid
-pid
-title
-date
-pid2
-title
-date
Controller.swift
I get the all the uids of the particular user is following
and then I try to fetch posts from those uids
fileprivate func fetchPosts() {
guard let uid = Auth.auth().currentUser?.uid else { return }
Database.fetchUserWithUID(uid: uid) { (user) in
self.fetchPostsWithUser(user: user)
}
}
fileprivate func fetchPostsWithUser(user: User) {
let uid = user.uid
let ref = Database.database().reference().child("posts").child(uid)
var query = ref.queryOrdered(byChild: "creationDate")
if posts.count > 0 {
let value = posts.last?.creationDate.timeIntervalSince1970
query = query.queryEnding(atValue: value)
}
query.queryLimited(toLast: 3).observeSingleEvent(of: .value, with: { (snapshot) in
guard var allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.reverse()
print("coint is , ", allObjects.count)
if allObjects.count < 2 {
self.isFinishedPaging = true
} else {
self.isFinishedPaging = false
}
if self.posts.count > 0 && allObjects.count > 0 {
allObjects.removeFirst()
}
allObjects.forEach({ (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
var post = Post(user: user, dictionary: dictionary)
post.id = snapshot.key
self.posts.append(post)
})
self.collectionView?.reloadData()
}) { (err) in
print(err)
}
}
extension
extension Database {
static func fetchUserWithUID(uid: String, completion: @escaping (User) -> ()) {
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String: Any] else { return }
let user = User(uid: uid, dictionary: userDictionary)
completion(user)
}) { (err) in
print("Failed to fetch user for posts:", err)
}
}
This code works but weirdly. I don't get the posts in right order. It is always giving me an output of randomly ordered posts.
What should I do?
swift firebase pagination swift4.1
swift firebase pagination swift4.1
asked Jun 26 '18 at 20:17
Api42Api42
65
65
I am not seeing anything really wrong with this code, and running it in a test app works consistently and does what it's supposed to in the correct order. However, I don't know how your creationDate string is stored in Firebase. It may also be easier instead of reversing allobjects to build an array, simply clearing the post array, then iterate over the snapshot.children and insert each new post at position 0. The will naturally put the most recent post at the top. Also, you need to ensure every posts node contains a creationDate child as if not, it won't sort properly.
– Jay
Jul 1 '18 at 12:56
add a comment |
I am not seeing anything really wrong with this code, and running it in a test app works consistently and does what it's supposed to in the correct order. However, I don't know how your creationDate string is stored in Firebase. It may also be easier instead of reversing allobjects to build an array, simply clearing the post array, then iterate over the snapshot.children and insert each new post at position 0. The will naturally put the most recent post at the top. Also, you need to ensure every posts node contains a creationDate child as if not, it won't sort properly.
– Jay
Jul 1 '18 at 12:56
I am not seeing anything really wrong with this code, and running it in a test app works consistently and does what it's supposed to in the correct order. However, I don't know how your creationDate string is stored in Firebase. It may also be easier instead of reversing allobjects to build an array, simply clearing the post array, then iterate over the snapshot.children and insert each new post at position 0. The will naturally put the most recent post at the top. Also, you need to ensure every posts node contains a creationDate child as if not, it won't sort properly.
– Jay
Jul 1 '18 at 12:56
I am not seeing anything really wrong with this code, and running it in a test app works consistently and does what it's supposed to in the correct order. However, I don't know how your creationDate string is stored in Firebase. It may also be easier instead of reversing allobjects to build an array, simply clearing the post array, then iterate over the snapshot.children and insert each new post at position 0. The will naturally put the most recent post at the top. Also, you need to ensure every posts node contains a creationDate child as if not, it won't sort properly.
– Jay
Jul 1 '18 at 12:56
add a comment |
3 Answers
3
active
oldest
votes
The reason you are getting the posts in a random order is because observeSingleEvent(of: ) fetches data from the database as first come first serve type of basis. So in the Firebase UI it may be alphabetically based on creation, but that doesn't mean it'll show up in the view when fetched. You'll have to use queryOrderedByKey) or (queryOrderedByValue) and sort it by the key/value (if you create posts by childByAutoID it will automatically be from oldest (on top) to newest when created.
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
add a comment |
Adding the follow lines in between self.posts.append and reloadData will solve your issue.
self.posts.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
add a comment |
1). Fetch first 5 data from firebase Here my Table name is "Tweets"
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryLimited(toLast: 5).observe(FIRDataEventType.value, with: { (tweets) in
for tweet in tweets.children{
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Store object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
2). After Fetch Next 5 data on your tableview scrolling.
// Here find last object key from array.
let key = self.tweetArray[self.tweetArray.count].key
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in
for tweet in tweets.children {
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Stored object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51050778%2fhow-do-i-paginate-using-swift-and-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The reason you are getting the posts in a random order is because observeSingleEvent(of: ) fetches data from the database as first come first serve type of basis. So in the Firebase UI it may be alphabetically based on creation, but that doesn't mean it'll show up in the view when fetched. You'll have to use queryOrderedByKey) or (queryOrderedByValue) and sort it by the key/value (if you create posts by childByAutoID it will automatically be from oldest (on top) to newest when created.
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
add a comment |
The reason you are getting the posts in a random order is because observeSingleEvent(of: ) fetches data from the database as first come first serve type of basis. So in the Firebase UI it may be alphabetically based on creation, but that doesn't mean it'll show up in the view when fetched. You'll have to use queryOrderedByKey) or (queryOrderedByValue) and sort it by the key/value (if you create posts by childByAutoID it will automatically be from oldest (on top) to newest when created.
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
add a comment |
The reason you are getting the posts in a random order is because observeSingleEvent(of: ) fetches data from the database as first come first serve type of basis. So in the Firebase UI it may be alphabetically based on creation, but that doesn't mean it'll show up in the view when fetched. You'll have to use queryOrderedByKey) or (queryOrderedByValue) and sort it by the key/value (if you create posts by childByAutoID it will automatically be from oldest (on top) to newest when created.
The reason you are getting the posts in a random order is because observeSingleEvent(of: ) fetches data from the database as first come first serve type of basis. So in the Firebase UI it may be alphabetically based on creation, but that doesn't mean it'll show up in the view when fetched. You'll have to use queryOrderedByKey) or (queryOrderedByValue) and sort it by the key/value (if you create posts by childByAutoID it will automatically be from oldest (on top) to newest when created.
answered Jun 27 '18 at 2:02
TorewinTorewin
446614
446614
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
add a comment |
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
Okay. Trying your solution. Thank you!
– Api42
Jun 27 '18 at 8:19
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
@Api42 The OP has ordering clearly defined in code ref.queryOrdered(byChild: "creationDate") so that will maintain the date order within the firebase snapshot.
– Jay
Jul 1 '18 at 12:45
add a comment |
Adding the follow lines in between self.posts.append and reloadData will solve your issue.
self.posts.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
add a comment |
Adding the follow lines in between self.posts.append and reloadData will solve your issue.
self.posts.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
add a comment |
Adding the follow lines in between self.posts.append and reloadData will solve your issue.
self.posts.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
Adding the follow lines in between self.posts.append and reloadData will solve your issue.
self.posts.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
answered Jan 2 at 3:39
富源Verve富源Verve
2917
2917
add a comment |
add a comment |
1). Fetch first 5 data from firebase Here my Table name is "Tweets"
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryLimited(toLast: 5).observe(FIRDataEventType.value, with: { (tweets) in
for tweet in tweets.children{
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Store object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
2). After Fetch Next 5 data on your tableview scrolling.
// Here find last object key from array.
let key = self.tweetArray[self.tweetArray.count].key
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in
for tweet in tweets.children {
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Stored object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
add a comment |
1). Fetch first 5 data from firebase Here my Table name is "Tweets"
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryLimited(toLast: 5).observe(FIRDataEventType.value, with: { (tweets) in
for tweet in tweets.children{
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Store object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
2). After Fetch Next 5 data on your tableview scrolling.
// Here find last object key from array.
let key = self.tweetArray[self.tweetArray.count].key
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in
for tweet in tweets.children {
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Stored object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
add a comment |
1). Fetch first 5 data from firebase Here my Table name is "Tweets"
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryLimited(toLast: 5).observe(FIRDataEventType.value, with: { (tweets) in
for tweet in tweets.children{
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Store object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
2). After Fetch Next 5 data on your tableview scrolling.
// Here find last object key from array.
let key = self.tweetArray[self.tweetArray.count].key
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in
for tweet in tweets.children {
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Stored object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
1). Fetch first 5 data from firebase Here my Table name is "Tweets"
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryLimited(toLast: 5).observe(FIRDataEventType.value, with: { (tweets) in
for tweet in tweets.children{
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Store object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
2). After Fetch Next 5 data on your tableview scrolling.
// Here find last object key from array.
let key = self.tweetArray[self.tweetArray.count].key
let FetchtweetRef = FIRDatabase.database().reference()
FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in
for tweet in tweets.children {
let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
// Stored object in Array
}
FetchtweetRef.child("Tweets").removeAllObservers()
})
answered Jan 2 at 5:26
Arjun PatelArjun Patel
14010
14010
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51050778%2fhow-do-i-paginate-using-swift-and-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

I am not seeing anything really wrong with this code, and running it in a test app works consistently and does what it's supposed to in the correct order. However, I don't know how your creationDate string is stored in Firebase. It may also be easier instead of reversing allobjects to build an array, simply clearing the post array, then iterate over the snapshot.children and insert each new post at position 0. The will naturally put the most recent post at the top. Also, you need to ensure every posts node contains a creationDate child as if not, it won't sort properly.
– Jay
Jul 1 '18 at 12:56