Skip to content

Instantly share code, notes, and snippets.

@michael-mckenna
Last active August 6, 2018 22:26
Show Gist options
  • Save michael-mckenna/7d4adb7817b6eca3c734d7c602785698 to your computer and use it in GitHub Desktop.
Save michael-mckenna/7d4adb7817b6eca3c734d7c602785698 to your computer and use it in GitHub Desktop.
var lastDoc: QueryDocumentSnapshot?
var listeners: [ListenerRegistration] = []
var query: Query = db.collection("channels").document("the doc id for this channel").collection("messages").limit(to: 25)
override func viewDidLoad() {
super.viewDidLoad()
addListener() // adds listener for first 25 messages
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// fetch nexst 25 items after scrolling to some point...
addListener()
}
func addListener() {
if let lastDoc = lastDoc {
query.start(afterDocument: lastDoc)
}
let messageListener = query
.addSnapshotListener(includeMetadataChanges: true, listener: { (querySnapshot, error) in
guard let snapshot = querySnapshot else {
print(error)
return
}
self.lastDoc = snapshot.documents.last
for diff in snapshot.documentChanges {
// update data source here
}
self.tableView.reloadData()
})
listeners.append(messageListener)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment