我创建的应用程序能够从firebase读取数据没问题。但是,当我尝试向其写入数据时,似乎并没有起作用。我检查了我的规则以确保" .write"设置为true,仍然没有看到任何弹出窗口。
尝试将事件写入firebase,如下所示:ref?.child(email).child("events").setValue("Test")
电子邮件变量有一个值,但要仔细检查它是不是奇怪的事我甚至尝试用电子邮件代替"测试"。不知道我哪里可能出错,任何帮助都是天赐之物!
完整的参考代码:
//
// NewEventToCreateVC.swift
// WSUStudentEvents
//
// Created by Colin Warn on 7/31/17.
// Copyright © 2017 Colin Warn. All rights reserved.
//
import UIKit
import FirebaseDatabase
import Firebase
class NewEventToCreateVC: UIViewController {
var eventToCreate: UserEvent?
@IBOutlet weak var titleOutlet: UILabel!
@IBOutlet weak var startTimeOutlet: UILabel!
@IBOutlet weak var endTimeOutlet: UILabel!
@IBOutlet weak var eventTypeOutlet: UILabel!
@IBOutlet weak var locationOutlet: UILabel!
@IBOutlet weak var descriptionOutlet: UITextView!
@IBOutlet weak var passwordOutlet: UILabel!
let defaults = UserDefaults.standard
var ref: DatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
ref = DatabaseReference().database.reference()
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
var startDateString = ""
var endDateString = ""
if let startTime = eventToCreate?.startTime, let endTime = eventToCreate?.endTime {
startDateString = formatter.string(from: startTime)
endDateString = formatter.string(from: endTime)
}
titleOutlet.text = eventToCreate?.eventName
startTimeOutlet.text = startDateString
endTimeOutlet.text = endDateString
eventTypeOutlet.text = eventToCreate?.eventType
locationOutlet.text = eventToCreate?.eventLocation
descriptionOutlet.text = eventToCreate?.description
if let password = eventToCreate?.password {
if password == "" {
passwordOutlet.text = "(no password)"
} else {
passwordOutlet.text = "Password: \(password)"
}
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
print("EVENT TO CREATE, new VC data transfer check")
print(eventToCreate)
print(eventToCreate?.eventName)
}
@IBAction func addEventPressed(_ sender: Any) {
uploadDataToFirebase()
}
@IBAction func backBtnPressed(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "AddEventViewModelVC") as! AddEventViewModelVC
if let eventName = eventToCreate?.eventName {
nextViewController.eventName = eventName
}
if let eventLocation = eventToCreate?.eventLocation {
nextViewController.eventLocation = eventLocation
}
nextViewController.startTime = eventToCreate?.startTime
nextViewController.endTime = eventToCreate?.endTime
if let description = eventToCreate?.description {
nextViewController.eventDescription = description
}
if let password = eventToCreate?.password {
nextViewController.password = password
}
if let eventType = eventToCreate?.eventType
{
nextViewController.eventType = eventType
}
nextViewController.eventEnum = .password
self.present(nextViewController, animated:true, completion: {
nextViewController.titleLabel.text = nextViewController.eventEnum.rawValue
nextViewController.textField.text = self.eventToCreate?.password
})
}
func uploadDataToFirebase(){
let email = defaults.object(forKey: "email") as! String
print(email)
//Email->Events->Data
//Set Email
//Set events
ref?.child(email).child("events").setValue("Test")
}
}
提前非常感谢你:
答案 0 :(得分:0)
将事情简化为最小代码通常有助于排除故障。例如,试试这个
@class xxxx
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
ref.child("events").setValue("test")
}
这将导致像这样的Firebase
root_ref
events: "test"
在Firebase调用中包含闭包也是一个好主意,这样你就可以捕获任何错误......所以你可以使用setValue代替setValue完成块(swift中的闭包)
let eventsRef = self.ref.child("events")
eventsRef.setValue("test", withCompletionBlock: { error in
if error != nil {
print(error)
}
})