gpt4 book ai didi

ios - 更新后在 Firebase 中查找项目的旧值

转载 作者:行者123 更新时间:2023-11-30 13:05:00 24 4
gpt4 key购买 nike

我正在制作一个简单的应用来使用 Firebase 进行 CRUD 工作 - 添加、更新和删除。我已完成所有这些任务,但我遇到了更新问题。

在更新子值时,我能够获取对正确用户的引用,并在 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)

上面的代码位于一个名为observePeople()的方法中,该方法在viewDidLoad方法期间被调用。 observePeople() 方法处理 ChildAdded、ChildRemoved 和 ChildChanged 通知。

如何正确导航到旧索引并将旧值更改为新值,以便 TableView 正确更新?

最佳答案

这是我的一个临时解决方案,它只能工作一次,之后就不再工作:

我创建了一个名为“ogFullName”的全局字符串变量,并在子添加代码期间将其设置为全名。然后在我的 child 更改的代码中,我将“ogFullName”设置为我要更改的元素的索引。对我来说用语言解释有点困难,所以这是代码:

var ogFullname:String?

func observePeople()
{
let ref = FIRDatabase.database().reference().child("Users")
ref.observeEventType(.ChildAdded, withBlock:
{ (snapshot) in

if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname")
{
let fullname = "\(firstname) \(lastname)"
self.ogFullname = fullname //the old name
...

在我的 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)"
if let ogname = self.ogFullname
{
if let originalIndex = self.names.indexOf(agnate) //finding the index of the unchanged name
{
self.names[originalIndex] = fullname //setting the old name to the changed one
}

此解决方案每次构建只能运行一次。之后,它会继续在控制台上显示更新的相同行为,但不在 TableView 上显示。

关于ios - 更新后在 Firebase 中查找项目的旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39500092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com