Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Last active May 1, 2017 11:04
Show Gist options
  • Select an option

  • Save seyhunak/98f13033deb775781cf585262eb8c94b to your computer and use it in GitHub Desktop.

Select an option

Save seyhunak/98f13033deb775781cf585262eb8c94b to your computer and use it in GitHub Desktop.
Firebase
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)
})
//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