Last active
August 26, 2018 16:13
-
-
Save michaelevensen/43ac12ad90302415963322d40d395a96 to your computer and use it in GitHub Desktop.
Firestore Mirror Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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