Last active
May 1, 2017 11:04
-
-
Save seyhunak/98f13033deb775781cf585262eb8c94b to your computer and use it in GitHub Desktop.
Firebase
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
| FIRDatabase.database().reference().child("conversations").child(location).observe(.value, with: { (snapshot) in | |
| if(snapshot.childrenCount > 1) { | |
| var removeCount = Int(snapshot.childrenCount) - 1 | |
| let enumerator = snapshot.children | |
| while let rest = enumerator.nextObject() as? FIRDataSnapshot { | |
| rest.ref.removeValue() | |
| removeCount -= 1 | |
| if(removeCount == 0) { | |
| break | |
| } | |
| } | |
| } | |
| completion(true) | |
| }, withCancel: { (error) in | |
| print(error) | |
| }) |
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
| //Downloads messages | |
| func fetchData() { | |
| Message.downloadAllMessages(forUserID: self.currentUser!.id, completion: {[weak weakSelf = self] (message) in | |
| weakSelf?.items.removeAll() | |
| weakSelf?.items.append(message) | |
| weakSelf?.items.sort{ $0.timestamp < $1.timestamp } | |
| if let itemsCount = weakSelf?.items.count { | |
| if(itemsCount > 1) { | |
| let range = 1..<itemsCount | |
| weakSelf?.items.removeSubrange(range) | |
| weakSelf?.tableView.reloadData() | |
| weakSelf?.tableView.scrollToRow(at: IndexPath.init(row: self.items.count - 1, section: 0), at: .bottom, animated: false) | |
| } | |
| } | |
| DispatchQueue.main.async { | |
| if let state = weakSelf?.items.isEmpty, state == false { | |
| weakSelf?.tableView.reloadData() | |
| weakSelf?.tableView.scrollToRow(at: IndexPath.init(row: self.items.count - 1, section: 0), at: .bottom, animated: false) | |
| } | |
| } | |
| }) | |
| Message.markMessagesRead(forUserID: self.currentUser!.id) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment