Skip to content

Instantly share code, notes, and snippets.

@michaelevensen
Last active August 26, 2018 16:13
Show Gist options
  • Save michaelevensen/43ac12ad90302415963322d40d395a96 to your computer and use it in GitHub Desktop.
Save michaelevensen/43ac12ad90302415963322d40d395a96 to your computer and use it in GitHub Desktop.
Firestore Mirror Database
// These methods removes the necessity of having to clear the array for each run as it just mirrors whatever the document data is.
self.array = snapshot!.documents.flatMap({Model(dictionary: $0.data())})
DispatchQueue.main.async {
self.tableView.reloadData()
}
// Or this is even better...
// Only check for document changes
snap.documentChanges.forEach {
diff in
// Check only for newly added documents
if diff.type == .added {
do {
let object = try Model(dictionary: diff.document.data(),
documentId: diff.document.documentID)
// Append
self.array.append(object)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch let error {
/* Handle object casting error */
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment