Created
October 19, 2016 04:26
-
-
Save rabimba/d247717bddda67c3a0d5e6f4580efc9c to your computer and use it in GitHub Desktop.
This file contains 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
deinit { | |
self.ref.child("messages").removeObserverWithHandle(_refHandle) | |
} | |
func configureDatabase() { | |
ref = FIRDatabase.database().reference() | |
// Listen for new messages in the Firebase database | |
_refHandle = self.ref.child("messages").observe(.childAdded, with: { [weak self] (snapshot) -> Void in | |
guard let strongSelf = self else { return } | |
strongSelf.messages.append(snapshot) | |
strongSelf.clientTable.insertRows(at: [IndexPath(row: strongSelf.messages.count-1, section: 0)], with: .automatic) | |
}) | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
// Dequeue cell | |
let cell = self.clientTable .dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) | |
// Unpack message from Firebase DataSnapshot | |
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row] | |
let message = messageSnapshot.value as! Dictionary<String, String> | |
let name = message[Constants.MessageFields.name] as String! | |
let text = message[Constants.MessageFields.text] as String! | |
cell.textLabel?.text = name! + ": " + text! | |
cell.imageView?.image = UIImage(named: "ic_account_circle") | |
if let photoURL = message[Constants.MessageFields.photoURL], let URL = URL(string: photoURL), let data = try? Data(contentsOf: URL) { | |
cell.imageView?.image = UIImage(data: data) | |
} | |
return cell | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been having issues with:
deinit { self.ref.child("messages").removeObserverWithHandle(_refHandle) }
where Xcode states: "'removeObserverWithHandle' has been renamed to 'removeObserver(withHandle:)'"
Maybe depreciated?