使用Firebase实时数据库向用户发送表单 [英] Send Form to users using Firebase Realtime-Database
问题描述
我已经解决了过去几周来遇到的一个巨大问题。长话短说,我正在尝试将阵列数据发送到Firebase数据库。当我按发送时,我将显示发送按钮当前正在发送的内容:
I am almost done fixing a huge problem I have been having the last few weeks. Long story short I'm trying to be able to send array data to my Firebase Database. I will show what the send button is currently sending when I press send:
我想要零是每个用户的ID(每行一个节点)。用户的数量和他们的身份由一个表确定,子值是数组中的值。
I would like the zero to be every user's id (one node for each row). The number of users and who they are is determined by a table and the child values to be the values in the array.
这是发送到firebase的函数的代码:
Here is the code for the function that sends to firebase:
@IBAction func sendButtonTapped(_ sender: Any) {
let companyNameC = companyNameTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let companyDescriptionC = companyDescriptionTextFieldConsiderations.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let today = Date()
let formatter1 = DateFormatter()
formatter1.dateFormat = "MMM d y"
print(formatter1.string(from: today))
let todaysDate = formatter1.string(from: today)
let storageRef = Storage.storage().reference(forURL: "MY STORAGE URL HERE")
let imageName = companyNameTextFieldConsiderations.text!
let storageCompanyRef = storageRef.child("Company_Image_Considerations").child("\(todaysDate)").child(imageName)
let companyDescriptionTextFieldText = companyDescriptionTextFieldConsiderations.text
let dateToStart = startDateTextFieldConsiderations.text
let dateToDecide = endDateTextFieldConsiderations.text
let companyRef = Database.database().reference().child("Considerations").child("\(todaysDate)").child(imageName)
let numberOfPeopleCells = ConsiderationsTestViewController.people
let considerationInfluencerRef = Database.database().reference().child("Considerations").child("\(todaysDate)").child(imageName).child("Users").child("\(numberOfPeopleCells.startIndex)")
let values = ["Feed_Quantity": "feedTFC", "Story_Quantity": "storyTFC", "Compensation": "compensationTFC"]
guard let imageSelected = self.CompanyImage.image else {
print ("Avatar is nil")
return
}
var dict: Dictionary<String, Any> = [
"Company Image": "",
"Company Description": companyDescriptionTextFieldText!,
"Start Date": dateToStart!,
"Decision Date": dateToDecide! ]
guard let imageData = imageSelected.jpegData(compressionQuality: 0.5) else {
return
}
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storageCompanyRef.putData(imageData, metadata: metadata, completion:
{ (StorageMetadata, error) in
if (error != nil) {
return
}
storageCompanyRef.downloadURL { (url, error) in
if let metadateImage = url?.absoluteString {
dict["Company Image"] = metadateImage
companyRef.updateChildValues(dict, withCompletionBlock: {
(error, ref) in
if error == nil {
print("Done")
return
}
}
)
}
}
storageRef.updateMetadata(metadata) { metadata, error in
if error != nil {
//Uh-oh, an error occurred!
} else {
// Updated metadata for 'images/forest.jpg' is returned
}
}
})
considerationInfluencerRef.updateChildValues(values as [AnyHashable : Any]) { (error, ref) in
if error != nil {
print(error ?? "")
return
}
self.navigationController?.popViewController(animated: true)
}
}
我似乎无法引用atIndexPath.row。然后,我将使用每个单元格中的UID创建一个节点,然后使每个indePath.row数据附加子级值。
I can't seem to be able to reference the atIndexPath.row. Then I would use the UID from each cell to create a node and then have each indePath.row data append the children values.
最佳做法是什么?
编辑:这是人的结构:
import UIKit
struct Person {
var Name: String?
var PostNumber: String?
var StoryNumber: String?
var Compensation: String?
var ProfileImageUrl: String?
var userID: String?
}
这是表代码:
extension ConsiderationsTestViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ConsiderationsTestViewController.people.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 72
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell, for: indexPath) as! ConsiderationsCell
let numberOfPeopleCells = ConsiderationsTestViewController.people[indexPath.row]
cell.nameLabelC.text = numberOfPeopleCells.Name
cell.feedLabelC.text = numberOfPeopleCells.PostNumber
cell.storyLabelC.text = numberOfPeopleCells.StoryNumber
cell.compensationLabelC.text = numberOfPeopleCells.Compensation
cell.userImage.loadImageUsingCacheWithUrlString(urlString: numberOfPeopleCells.ProfileImageUrl!)
cell.userImage.layer.cornerRadius = 25
cell.nameLabelC.numberOfLines = 0
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
ConsiderationsTestViewController.people.remove(at: indexPath.row)
// handle delete (by removing the data from your array and updating the tableview)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
}
编辑:
这里是一个有3个用户的数组示例:
Here is an example of the array when there is 3 users:
3 elements
▿ 0 : Person
▿ Name : Optional<String>
- some : "Person's Name1"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "1"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "B2Z4DlZ8RucvEQhz2NSUkquqc5P2"
▿ 1 : Person
▿ Name : Optional<String>
- some : "Person's Name2"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "11"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "Lbn9HL1WIpZTMFzpGWAXy7Ra0EA2"
▿ 2 : Person
▿ Name : Optional<String>
- some : "Person's Name3"
▿ PostNumber : Optional<String>
- some : "1"
▿ StoryNumber : Optional<String>
- some : "1"
▿ Compensation : Optional<String>
- some : "1"
▿ ProfileImageUrl : Optional<String>
- some : "PROFILEIMAGE URL HERE"
▿ userID : Optional<String>
- some : "NE6WVUfF6WQjJT9eeVSJgfvrrZW2"
推荐答案
如果这是您的目标(如您的问题所述) )
If this is your goal (as stated in your question)
我希望零为用户的ID
I would like the zero to be the user's id
行
let considerationInfluencerRef = Database.database().reference().child("Considerations")
.child("\(todaysDate)")
.child(imageName)
.child("Users")
.child("\(numberOfPeopleCells.startIndex)")
从此更改结束子节点
.child("\(numberOfPeopleCells.startIndex)")
为此
.child(Auth.auth().currentUser.uid)
要稍微扩展一点,您应该使用dataSource数组来支持tableView
To expand a little, you should be using a dataSource array to back your tableView
var myDatasource = [Person]()
因此,如果您有三个人(使用您的结构)
so if you have three people (using your structure)
myDatasource[0] = person 0
myDatasource[1] = person 1
myDatasource[2] = person 2
因此,只需遍历数组,获取人员及其uid并写入数据即可。像这样的东西(不要复制粘贴,在我的iPad上完成)
So just iterate over the array, get the person and their uid and write the data. Something like this (don't copy paste, did this on my iPad)
myDatasource.forEach { person in
let uid = person.userID
let dict = [
"name": person.name,
"postNumber": person.postNumber
]
let ref = let considerationInfluencerRef = Database.database().......child(uid)
ref.setData(dict)
}
这篇关于使用Firebase实时数据库向用户发送表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!