私は、FirebaseでCRUDを行うための簡単なアプリを作っています - 追加、更新、削除。私は更新の問題があることを除いて、すべてを完了しました。更新後のFirebaseでアイテムの古い値を見つける
子供の値を更新するときに適切なユーザーへの参照を取得し、firebaseコンソールで正常に更新することができました。これは私がそのために使用するコードです:
let ref = FIRDatabase.database().reference().child("Users")
ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in
guard let dictionary = snapshot.value as? [String:AnyObject] else { return }
if self.firstNameField.text == dictionary["firstname"] as? String
{
if let lastname = dictionary["lastname"]
{
print("Here is the lastname of the current person \(lastname)")
guard let firstname = self.firstNameField.text, lastname = self.lastNameField.text, dateOfBirth = self.dateOfBirthField.text, zipcode = Int(self.zipcodeField.text!) else
{
print("Form not valid")
return
}
let properties: [String: AnyObject] = ["firstname": firstname, "lastname": lastname, "Date of Birth": dateOfBirth, "Zipcode": zipcode]
let currentKey = snapshot.key
ref.child(currentKey).updateChildValues(properties, withCompletionBlock: { (error, ref) in
if error != nil
{
print("We have an error updating the data \(error)")
}
print("We were able to update the entry in the reference \(ref)")
})
}
}
}, withCancelBlock: nil)
私の問題は、すぐに私はEditViewControllerを解任として適切なインデックスを更新していないTableViewControllerです。それに対してtableview.reloadData()メソッドが機能していないので、私はChildChangedメソッドでそれを修正する必要があると考えています。
ref.observeEventType(.ChildChanged, withBlock: { (snapshot) in
print("One of the entries were changed so we're reloading the table view")
if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname")
{
let fullname = "\(firstname) \(lastname)"
self.names[I_Need_This_Index] = fullname
print(fullname)
dispatch_async(dispatch_get_main_queue())
{
self.tableView.reloadData()
}
}
}, withCancelBlock: nil)
コードは、上記のviewDidLoadメソッド中に呼び出されるobservePeople()と呼ばれる方法である:ここではそのためのコードです。 observePeople()メソッドは、ChildAdded、ChildRemoved、およびChildChanged通知を処理しました。
古いインデックスを正しくナビゲートし、古い値を新しい値に変更して、テーブルビューが正しく更新されるようにするにはどうすればよいですか?私は「ogFullName」と呼ばれるグローバル文字列変数を作成し、子供がコードを追加した時に完全な名前に設定し
:
値を更新するときに 'dataSource [indexOfCellThatChanged]'を変更します.-> reloadTableViewを呼び出します。 – Dravidian
私はそれを何に変更するのですか?そして、私は他のView Controllerからどのようにアクセスできますか? –