Here is my JSON Structure (Ignore the username to email part of it, that is for a test)
{
"Username" : {
"Username" : {
"email" : "test@gmail.com"
}
},
"uid_0" : {
"Song List" : [ "The National Anthem" ]
}
}
I am making a very simplistic app using Swift and Xcode. Basically, the user signs into their account after they register. From there the user is able to choose their favorite songs from a band's discography into an empty array. The user is then able to click a button that segues them to a table view controller that contains the songs they added. The way the user adds songs is through a view controller that contains labels that have the song names and buttons next to each song name. When the user clicks the button the label is added to the array.
I want to be able to save the array to firebase so that each user will have their own favorite songs. I have done a decent amount of research and can not find anything to steer me in the right direction as this is incredibly simple. The labels have no keys and are literally just labels being added to an array.
I have the firebase storage and database pods installed as well and I am able to upload images to fire base and have the user save that image to their specific account but how to do the same thing for an array? Here is my code for the array so you can get an idea of how it works.
import UIKit
import Firebase
import FirebaseStorage
import FirebaseAuth
import LocalAuthentication
import FirebaseDatabase
//var refSongs: DatabaseReference! {
// return Database.database().reference()
//}
var list = [String]()
class SongsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
@IBAction func saveSongs(_ sender: Any) {
//upload array to firebase storage
//let defaults = UserDefaults.standard.set(list, forKey: "KeySave")
//addSongs()
// create add list reference in fire database
// let addListRef = refSongs.child("Song List").childByAutoId()
// let addList1 = list
// addListRef.setValue(addList1)
let ref = self.ref.child("uid_0").child("Song List")
// let songArray = [list] *** Removed this
ref.setValue(list) //changed to list
retriveList()
}
func retriveList() {
let ref = self.ref.child("uid_0").child("Song List")
ref.observeSingleEvent(of: .value, with: {snapshot in
var mySongArray = [String]()
for child in snapshot.children {
let snap = child as! DataSnapshot
let song = snap.value as! String //!! Program crashes here!!
mySongArray.append(song)
}
print(mySongArray)
})
}
// add labels to array
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return (cell)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
list.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}
@IBOutlet weak var myTableView: UITableView!
}