How sort the documents in a collection randomly.
This is the function I use to get the documents, how can I sort them randomly?
func fetchExplore() {
let query = COLLECTION_POSTS.limit(to: 6)
if let last = lastDoc {
let next = query.start(afterDocument: last)
next.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents, !documents.isEmpty else { return }
self.lastDoc = snapshot?.documents.last
self.posts.append(contentsOf: documents.compactMap({ try? $0.data(as: Post.self) }))
}
} else {
query.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents else { return }
self.posts = documents.compactMap({ try? $0.data(as: Post.self) })
self.lastDoc = snapshot?.documents.last
}
}
print("DEBUG: did fetch posts Explore")
}