问题描述
我正在尝试将数据保存到我的Firebase数据库,但是我遇到了一些问题。这里是我用户按下提交按钮时的代码:
var ref = FIRDatabase.database()。reference )
var user = FIRAuth.auth()?currentUser
@IBOutlet weak var titleEntry:UITextField!
@IBOutlet weak var townAndZip:UITextField!
@IBOutlet weak var pickPrice:UISegmentedControl!
@IBOutlet弱变量pickDuration:UISegmentedControl!
@IBAction func tapBackground(sender:AnyObject){
view.endEditing(true)
}
@IBAction func postTask(sender:AnyObject) {($ title){
if((titleEntry.text?.isEmpty)!= nil&&(townAndZip.text?.isEmpty)!= nil){
var price: String = pickPrice.titleForSegmentAtIndex(pickPrice.selectedSegmentIndex)!
var duration:String = pickDuration.titleForSegmentAtIndex(pickDuration.selectedSegmentIndex)!
self.ref.setValue([title:titleEntry.text!))
self.ref.child(tasks)。child(user!.uid).setValue [price:price])
self.ref.child(tasks)。child(user!.uid).setValue([duration:duration])
self.ref.child (任务)。child(user!.uid).setValue([town_zip:townAndZip.text!))
self.performSegueWithIdentifier(postAdd,sender:self)
print(Posted!)
} else {
let alert = UIAlertController(title:Error,message:请确保你填写了所有东西。,preferredStyle:UIAlertControllerStyle .Alert)
alert.addAction(UIAlertAction(title:Understood!,style:UIAlertActionStyle.Default,handler:nil))
self.presentViewController(alert,animated:true,completion:nil)
$ b无论我输入什么内容,应用程序都会执行segue并且不会更新我的Firebase数据库。此外,如果我只输入其中一个字段的数据,它仍然执行搜索并不显示警报。我认为这是Firebase本身的问题,但可能是我找不到的错误。任何帮助将不胜感激。解决方案您是否尝试检查规则选项卡? (在数据选项卡后面)。
如果你还没有授权,firebase会不允许你写数据。为了省略,你可以改变规则到这个
{
规则:{
。 read:true,
.write:true
}
}
$ b $但是:从现在开始,即使没有授权,你的应用程序的任何用户都可以修改你的数据库,所以我建议你使用它作为临时黑客。
I'm trying to test saving data to my Firebase Database but I've been having some problems. Here is the code I have for when the user presses the Submit button:
var ref = FIRDatabase.database().reference()
var user = FIRAuth.auth()?.currentUser
@IBOutlet weak var titleEntry: UITextField!
@IBOutlet weak var townAndZip: UITextField!
@IBOutlet weak var pickPrice: UISegmentedControl!
@IBOutlet weak var pickDuration: UISegmentedControl!
@IBAction func tapBackground(sender: AnyObject) {
view.endEditing(true)
}
@IBAction func postTask(sender: AnyObject) {
if ((titleEntry.text?.isEmpty) != nil && (townAndZip.text?.isEmpty) != nil) {
var price:String = pickPrice.titleForSegmentAtIndex(pickPrice.selectedSegmentIndex)!
var duration:String = pickDuration.titleForSegmentAtIndex(pickDuration.selectedSegmentIndex)!
self.ref.setValue(["title": titleEntry.text!])
self.ref.child("tasks").child(user!.uid).setValue(["price": price])
self.ref.child("tasks").child(user!.uid).setValue(["duration": duration])
self.ref.child("tasks").child(user!.uid).setValue(["town_zip": townAndZip.text!])
self.performSegueWithIdentifier("postAdd", sender: self)
print("Posted!")
} else {
let alert = UIAlertController(title: "Error", message: "Please make sure you filled in everything.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Understood!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
No matter what I type in, the app performs the segue and does not update in my Firebase database. Also, if I only input data for one of the fields, it still performs the segue and doesn't display the alert. I thought it was a problem with Firebase itself but it's likely a mistake I just can't find. Any help would be greatly appreciated.
解决方案 Did you try to check the "Rules" tab? (It's right after the "Data" tab).
If you have not authorised, firebase will not allow you to write data. To omit that you can changes the rules to this
{
"rules": {
".read": "true",
".write": "true"
}
}
But: from now on any user of your app even without authorization can modify your database, I would recommend you to use it as a temp hack.
这篇关于setValue方法不会更新到Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!