Всем привет !!! подскажите пожалуйста при использовании приложения написанного по курсу Firebase возникает проблема с сохранением одного и того-же значения . Например: при сохранения в tasks слово “test” второй раз он его сохранить не дает. Подскажите как это исправить?
Курс №12 Firebase
Arsen
#4
import UIKit
import Firebase
class TasksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var user: User!
var ref: FIRDatabaseReference!
var tasks = Array<Task>()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
guard let currentUser = FIRAuth.auth()?.currentUser else {
return
}
user = User(user: currentUser)
ref = FIRDatabase.database().reference(withPath: "users").child(String(user.uid)).child("tasks")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ref.observe(.value, with: {[weak self] (snapshot) in
var _tasks = Array<Task>()
for item in snapshot.children {
let task = Task(snapshot: item as! FIRDataSnapshot)
_tasks.append(task)
}
self?.tasks = _tasks
self?.tableView.reloadData()
})
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
ref.removeAllObservers()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .clear
let task = tasks[indexPath.row]
cell.textLabel?.textColor = .white
let tasksTitle = task.title
let isComplited = task.completed
cell.textLabel?.text = tasksTitle
taggleComplition(cell, isComplited: isComplited)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let task = tasks[indexPath.row]
task.ref?.removeValue()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else {return}
let task = tasks[indexPath.row]
let isComplited = !task.completed
taggleComplition(cell, isComplited: isComplited)
task.ref?.updateChildValues(["completed" : isComplited])
}
func taggleComplition (_ cell: UITableViewCell, isComplited: Bool) {
cell.accessoryType = isComplited ? .checkmark : .none
}
@IBAction func addTapped(_ sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "Новая Задача", message: "Добавить Задачу", preferredStyle: .alert)
alertController.addTextField()
let save = UIAlertAction(title: "Сохранить", style: .default) { [weak self] _ in
guard let textField = alertController.textFields?.first, textField.text != "" else {return}
let task = Task(title: textField.text!, userId: (self?.user.uid)!)
let taskRef = self?.ref.child(task.title.lowercased())
taskRef?.setValue(task.convertToDictionary())
}
let cancel = UIAlertAction(title: "Отмена", style: .default, handler: nil)
alertController.addAction(save)
alertController.addAction(cancel)
present(alertController, animated: true, completion: nil)
}
@IBAction func signOutTapped(_ sender: UIBarButtonItem) {
do {
try FIRAuth.auth()?.signOut()
} catch {
print(error.localizedDescription)
}
dismiss(animated: true, completion: nil)
}
}