Swift Firebase completion handler not working, not fetching Data - ios

I have my function here which is supposed to pull all the locations under the child "launch" in my database.
import UIKit
import Firebase
import FirebaseDatabase
class SearchSelectVC: UIViewController, UIPickerViewDelegate , UIPickerViewDataSource{
#IBOutlet weak var locationPicker: UIPickerView!
#IBOutlet weak var datePicker: UIPickerView!
#IBOutlet weak var locationLB: UILabel!
#IBOutlet weak var dateLB: UILabel!
var locData: [String] = [String]()
var dateData : [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
initPickers()
getloc(onCompletion: { (data) in
print(data)
self.locData = (data)
})
}
func getDate(onCompletion: #escaping ([String]) -> ()) {
var data : [String] = []
let selectedLoc = locationLB.text
var dateRef: DatabaseReference!
dateRef = Database.database().reference().child("launch").child((selectedLoc)!)
dateRef.observe(.value) { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
data.append(key)
print("date = \(key)")
//print(data)
}
onCompletion(data)
}
}
func getLoc(onCompletion: #escaping ([String]) -> Void){
print("here3")
var data : [String] = []
var locRef: DatabaseReference!
locRef = Database.database().reference()
print("here4")
locRef.child("launch").observe(.value) { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
print(snap)
print(key)
data.append(key)
print("location = \(key)")
//print(data)
}
onCompletion(data)
}
}
func initPickers(){
locationPicker.delegate = self
locationPicker.dataSource = self
datePicker.delegate = self
datePicker.dataSource = self
}
}
None of the "here" statements get printed out, neither the snapshot of key or the data which is being collected. Im not really sure whats wrong with my code. Here is a picture of my database
Thanks!

Related

Swift/Firebase - Unable to retrieve data in order that it was entered?

Having a problem retrieving data from Firebase in the same order that it was entered. I have tried this a number of ways using different variations of .valueAdded & .value to get this back in the same order but having no luck. Perhaps the way in which I am modelling this data is incorrect? Any help with this would be great. Thanks.
This is my Firebase Structure :
This is my data model:
struct RentalObjects {
var title = [String]()
var rentalType = [String]()
var dateAval = [String]()
var location = [String]()
var price = [String]()
var bond = [String]()
var pets = [String]()
var descripton = [String]()
}
This is my table view VC :
import UIKit
import FirebaseDatabase
class RentalTableViewVC: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var rentalImage: UIImageView!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var rentalTitle: UILabel!
#IBOutlet weak var rentalPrice: UILabel!
var rentalsObject = RentalObjects()
var databaseRef:DatabaseReference?
var handle: DatabaseHandle?
var arrayOfTitles = [String?]()
var arrayOfBond = [String?]()
var arrayOfDateAval = [String?]()
var arrayOfDes = [String?]()
var arrayOfLocation = [String?]()
var arrayOfPets = [String?]()
var arrayOfPrice = [String?]()
var arrayOfRentalType = [String?]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rentalsObject.title.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = ("Title: \(rentalsObject.title[indexPath.row]), DateAval: \(rentalsObject.dateAval[indexPath.row])")
return cell
}
#IBAction func backPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.dataSource = self
databaseRef = Database.database().reference().child("Rentals")
databaseRef?.observe(.childAdded, with: { (snapshot) in
if let dictonary = snapshot.value as? [String: AnyObject] {
print(snapshot)
switch snapshot.key {
case "bond" :
_ = dictonary.map{self.rentalsObject.bond.append(($0.value as? String)!)}
// print(self.arrayOfBond)
case "dateAval" :
_ = dictonary.map{self.rentalsObject.dateAval.append(($0.value as? String)!)}
case "description" :
_ = dictonary.map{self.rentalsObject.descripton.append(($0.value as? String)!)}
case "location" :
_ = dictonary.map{self.rentalsObject.location.append(($0.value as? String)!)}
case "pets" :
_ = dictonary.map{self.rentalsObject.pets.append(($0.value as? String)!)}
case "price" :
_ = dictonary.map{self.rentalsObject.price.append(($0.value as? String)!)}
case "rentalType" :
_ = dictonary.map{self.rentalsObject.rentalType.append(($0.value as? String)!)}
case "title" :
_ = dictonary.map{self.rentalsObject.title.append(($0.value as? String)!)}
print(self.rentalsObject.title)
// _ = dictonary.map{self.arrayOfTitles.append($0.value as? String)}
// print(self.arrayOfTitles)
default:
break
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
}
This is an example of the output from my tableView. I am trying to get this out in the same order it came in, in this example, 1,2,3,4,5. When I add in data from the other variables to this output they all become mixed up for some reason.
This is the code where the data is sent to firebase:
class AvertisingVC: UIViewController {
#IBOutlet weak var titleField: UITextField!
#IBOutlet weak var rentalTypeField: UITextField!
#IBOutlet weak var dateField: UITextField!
#IBOutlet weak var locationField: UITextField!
#IBOutlet weak var priceField: UITextField!
#IBOutlet weak var bondField: UITextField!
#IBOutlet weak var petsAllowedField: UITextField!
#IBOutlet weak var detailedDescription: UITextField!
var databaseRef:DatabaseReference? //reference to firebase dba
override func viewDidLoad() {
super.viewDidLoad()
databaseRef = Database.database().reference().child("Rentals") //can add .child(string:root) to add root dir to dba
}
#IBAction func backBtnPressed(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func submitForm(_ sender: Any) { // Send data to firebase on submit
if titleField.text != nil {
databaseRef?.child("title").childByAutoId().setValue(titleField.text)
titleField.text = ""
}
if rentalTypeField.text != nil {
databaseRef?.child("rentalType").childByAutoId().setValue(rentalTypeField.text)
rentalTypeField.text = ""
}
if dateField.text != nil {
databaseRef?.child("dateAval").childByAutoId().setValue(dateField.text)
dateField.text = ""
}
if locationField.text != nil {
databaseRef?.child("location").childByAutoId().setValue(locationField.text)
locationField.text = ""
}
if priceField.text != nil {
databaseRef?.child("price").childByAutoId().setValue(priceField.text)
priceField.text = ""
}
if bondField.text != nil {
databaseRef?.child("bond").childByAutoId().setValue(bondField.text)
bondField.text = ""
}
if petsAllowedField.text != nil {
databaseRef?.child("pets").childByAutoId().setValue(petsAllowedField.text)
petsAllowedField.text = ""
}
if detailedDescription.text != nil {
databaseRef?.child("description").childByAutoId().setValue(detailedDescription.text)
detailedDescription.text = ""
}
let alertController = UIAlertController(title: "Success!", message: "You have successfully listed a rental", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Close Alert", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
When data is interpreted from network data, swift doesn't guarantee the order of the interpreted values if they're ordered in an array or dictionary. You'd have to order the values yourself, or update a cache device-side, and sort that cache each time it is updated.
There's many ways to this simply, or intricately for various use-cases. But looking at your's, i'd add a sort on wherever you're reading the tableView's datasource, before you call the delegate and datasource methods. Taking your example, if you sorted by date:
override func viewDidLoad() {
super.viewDidLoad()
databaseRef = Database.database().reference().child("Rentals")
databaseRef?.observe(.childAdded, with: { (snapshot) in
if let dictonary = snapshot.value as? [String: AnyObject] {
print(snapshot)
switch snapshot.key {
case "bond" :
_ = dictonary.map{self.rentalsObject.bond.append(($0.value as? String)!)}
// print(self.arrayOfBond)
case "dateAval" :
_ = dictonary.map{self.rentalsObject.dateAval.append(($0.value as? String)!)}
case "description" :
_ = dictonary.map{self.rentalsObject.descripton.append(($0.value as? String)!)}
case "location" :
_ = dictonary.map{self.rentalsObject.location.append(($0.value as? String)!)}
case "pets" :
_ = dictonary.map{self.rentalsObject.pets.append(($0.value as? String)!)}
case "price" :
_ = dictonary.map{self.rentalsObject.price.append(($0.value as? String)!)}
case "rentalType" :
_ = dictonary.map{self.rentalsObject.rentalType.append(($0.value as? String)!)}
case "title" :
_ = dictonary.map{self.rentalsObject.title.append(($0.value as? String)!)}
print(self.rentalsObject.title)
// _ = dictonary.map{self.arrayOfTitles.append($0.value as? String)}
// print(self.arrayOfTitles)
default:
break
}
}
whateverCollectionIsYourInCacheDataSource = whateverCollectionIsYourInCacheDataSource.sorted(by: { ($0, $1) -> Bool in
return $0 < $1 //If you want to sort them by their date value. Make sure the dates are properly interpreted from whatever format they're saved in remotely into the Date type.
})
DispatchQueue.main.async {
//No need to call .reloadData and these methods in viewDidLoad, unless you REALLY have to (ie: your data is modified on purpose after being loaded initially). Calling these methods loads up the data anyways, so just call these whenever your data is downloaded, converted and sorted properly.
tableView.dataSource = self
tableView.dataSource = self
}
})
}
Hope it helps you on the right path. Look up network-based tableview / collectionview tutorials, they can be very helpful.

How would I pull the key from firebase of an already created cell that someone tapped on

How would I pull the key from firebase of an already created cell (I'm using TableView) that someone tapped on, and create that as a local var? I want to know this so I can transfer the key into the next view so I can make a generic storyboard but the text fields change with the key. This also means I want to set the data in one view controller then send it to another which if I'm using the same database (I am) for this I would need it to be edited on the second view controller.
I've already got this much on the second view controller (for firebase)
refObjective = Database.database().reference().child("objective");
let bodyFire = refObjective.childByAutoId().child("body")
bodyFire.observeSingleEvent(of: .value, with: { (snapshot) in
let bodyOnFire = snapshot.value as? String
self.text?.text = bodyOnFire
})
and the entire view first/tableview controller
// WeeklyObjectivesEditorViewController.swift
// Studioso
//
// Created by Charles Porth on 8/29/17.
// Copyright © 2017 Studioso. All rights reserved.
//
import Foundation
import UIKit
import FirebaseCore
import FirebaseDatabase
class WeeklyObjectivesEditorViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var View2: UITabBarItem!
#IBOutlet weak var toolbar1: UITextField!
//defining firebase reference var
var refToolbars: DatabaseReference!
var refToolbars2: DatabaseReference!
let arrayOfIDs: NSMutableArray = [""]
#IBOutlet weak var tableViewToolbar: UITableView!
//list to store all the artist
var toolbarList = [ToolbarModel]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toolbarList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
//creating a cell using the custom class
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
//the artist object
let toolbar: ToolbarModel
//getting the artist of selected position
toolbar = toolbarList[indexPath.row]
//adding values to labels
cell.label.text = toolbar.toolbar1
//returning cell
return cell
}
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var Remove: UIBarButtonItem!
#IBOutlet weak var add: UIBarButtonItem!
#IBAction func buttonToolbar(_ sender: UIButton) {
addToolbar()
restructureDBKeys()
//reloading the tableview
self.tableViewToolbar.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Add this
tableViewToolbar.delegate = self
tableViewToolbar.dataSource = self
//getting a reference to the node artists
refToolbars = Database.database().reference().child("toolbars");
print(selectedKey + "this is the selected key")
dataPassedMedium = selectedKey
let bodyFire = refToolbars.childByAutoId().child("body")
//this is how you get it
refToolbars.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull{
//Handle errors
}
else{
self.arrayOfIDs.removeAllObjects()
if let dict = snapshot.value as? NSDictionary{
//dict.allKeys is all the IDs
for ID in dict.allKeys{
self.arrayOfIDs.add(ID as! String) //Will always be a string in Firebase
}
}
}
})
//observing the data changes
refToolbars.observe(DataEventType.value, with: { (snapshot) in
//if the reference have some values
if snapshot.childrenCount > 0 {
//clearing the list
self.toolbarList.removeAll()
//iterating through all the values
for toolbar in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
let toolbarObject = toolbar.value as? [String: AnyObject]
let toolbarId = toolbarObject?["id"]
let toolbarToolbar1 = toolbarObject?["toolbar1"]
let toolbarBody = toolbarObject?["body"]
let toolbarTitle = toolbarObject?["title"]
// creating artist object with model and fetched values
//creating artist object with model and fetched values
let toolbar = ToolbarModel(id: toolbarId as! String?, toolbar1: toolbarToolbar1 as! String?, title: toolbarTitle as! String?, body: toolbarBody as! String?)
//appending it to list
self.toolbarList.append(toolbar)
}
//reloading the tableview
self.tableViewToolbar.reloadData()
}
})
}
func addToolbar(){
//generating a new key inside artists node
//and also getting the generated key
let key = refToolbars.childByAutoId().key
//creating artist with the given values
let toolbar = ["id":key,
"toolbar1": toolbar1.text! as String,
]
//adding the artist inside the generated unique key
refToolbars.child(key).setValue(toolbar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
{
var valueToPass:String! // #1 i added this one first
var selectedKey: String!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)! as! ViewControllerTableViewCell
// valueToPass = currentCell.label.text
//the artist object
//getting the artist of selected position
// toolbar = toolbarList[indexPath.row]
let destinationVC = AddNewObjectiveViewController()
destinationVC.transferRow = selectedKey
selectedKey = (arrayOfIDs[indexPath.row] as? String)!
print(selectedKey + "this is the selected key# selected")
print(dataPassedMedium + "this is the dataPassedMedium key# selected")
dataPassedMedium = selectedKey
self.performSegue(withIdentifier: "weeklyadd", sender: self)
}
func restructureDBKeys() {
// let arrayOfIDs: NSMutableArray = [""]
//this is how you get it
refToolbars.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull{
//Handle errors
}
else{
self.arrayOfIDs.removeAllObjects()
if let dict = snapshot.value as? NSDictionary{
//dict.allKeys is all the IDs
for ID in dict.allKeys{
self.arrayOfIDs.add(ID as! String) //Will always be a string in Firebase
}
}
}
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// #2 used if let as? UINavigationController
let destinationVC = navigationController?.topViewController as? AddNewObjectiveViewController
// destinationVC?.transferRow = valueToPass
destinationVC?.transferRow = selectedKey
print("data should be transfered" )
dataPassedMedium = selectedKey
if let navigationController = segue.destination as? UINavigationController {
let destinationVC = navigationController.topViewController as? AddNewObjectiveViewController
destinationVC?.transferRow = selectedKey
print("data should be transfered" )
}
}
}
var dataPassedMedium: String!
and the entire view second controller
//
// AddNewObjective.swift
// Studioso
//
// Created by Charles Porth on 9/21/17.
// Copyright © 2017 Studioso. All rights reserved.
//
import Foundation
import QuartzCore
import UIKit
import FirebaseCore
import FirebaseDatabase
class AddNewObjectiveViewController: UIViewController {
#IBOutlet var text : UITextView?
#IBOutlet var textfield : UITextField!
var transferRow = String()
//defining firebase reference var
var refObjective: DatabaseReference!
#IBAction func Save(_ sender: Any) {
print(transferRow + "String has been tranfered via tranferRow during save" )
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//not perimantent
self.text?.layer.borderWidth = 1
self.text?.layer.borderColor = UIColor.red.cgColor
refObjective = Database.database().reference().child("toolbar");
//transferRow = WeeklyObjectivesEditorViewController().selectedKey
transferRow = dataPassedMedium
// refObjective = Database.database().reference().child("toolbars");
// let bodyFire = refObjective.child("-KwSYtZU_EX1vFFeTI4g").child("body")
print(transferRow + "String has been tranfered via tranferRow")
let delayInSeconds = 4.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) {
}
}
func addObjective(){
//generating a new key inside artists node
//and also getting the generated key
//creating artist with the given values
let objective = ["id": self.transferRow,
"body": self.text?.text! as! String,
"title": self.textfield.text! as String
] as [String : Any]
//adding the artist inside the generated unique key
self.self.refObjective.child(self.transferRow).setValue(objective)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
To gather the keys to use in a tableView you can use a dictionary or an array - your preference.
//This is the key that is desired.
let bodyFire = refObjective.childByAutoId().child("body")
In didSelectRowAt will be when you select the key:
let selectedKey = arrayOfIDs[indexPath.row] as? String
//
// WeeklyObjectivesEditorViewController.swift
// Studioso
//
// Created by Charles Porth on 8/29/17.
// Copyright © 2017 Studioso. All rights reserved.
//
import Foundation
import UIKit
import FirebaseCore
import FirebaseDatabase
class WeeklyObjectivesEditorView2ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var toolbar1: UITextField!
#IBOutlet weak var View1: UITabBarItem!
//defining firebase reference var
var refToolbars2: DatabaseReference!
let arrayOfIDs: NSMutableArray = [""]
// #IBOutlet weak var label: UILabel!
#IBOutlet weak var tableViewToolbar: UITableView!
//list to store all the artist
var toolbarList = [View2ToolbarModel]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toolbarList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
//creating a cell using the custom class
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! ViewControllerTableViewCell2
//the artist object
let toolbar: View2ToolbarModel
//getting the artist of selected position
toolbar = toolbarList[indexPath.row]
//adding values to labels
cell1.label.text = toolbar.toolbar1
//returning cell
return cell1
}
#IBOutlet weak var scrollView: UIScrollView!
//1 == up
// #IBOutlet weak var toolbar2: UITextField!
// #IBOutlet weak var toolbar3: UITextField!
// #IBOutlet weak var toolbar4: UITextField!
// #IBOutlet weak var toolbar5: UITextField!
// #IBOutlet weak var toolbar6: UITextField!
// #IBOutlet weak var toolbar7: UITextField!
// #IBOutlet weak var toolbar8: UITextField!
// #IBOutlet weak var toolbar9: UITextField!
// #IBOutlet weak var toolbar10: UITextField!
// #IBOutlet weak var toolbar11: UITextField!
// #IBOutlet weak var toolbar12: UITextField!
// #IBOutlet weak var toolbar13: UITextField!
#IBOutlet weak var Remove: UIBarButtonItem!
#IBOutlet weak var add: UIBarButtonItem!
/*
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "WeeklyObjectivesViewController" {
let WeeklyObjectivesViewController = self.storyboard?.instantiateViewController(withIdentifier: "WeeklyObjectivesViewController") as! WeeklyObjectivesViewController
*/
//https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2
#IBAction func buttonToolbar(_ sender: UIButton) {
addToolbar()
//reloading the tableview
self.tableViewToolbar.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
refObjective(of: .value, with: { (snapshot) in
if snapshot.value is NSNull{
//Handle errors
}
else{
arrayOfIDs.removeAllObjects()
if let dict = snapshot.value as? NSDictionary{
//dict.allKeys is all the IDs
for ID in dict.allKeys{
arrayOfIDs.add(ID as! String) //Will always be a string in Firebase
self.tableView.reloadData()
}
}
}
})
// Add this
tableViewToolbar.delegate = self
tableViewToolbar.dataSource = self
//getting a reference to the node artists
refToolbars2 = Database.database().reference().child("toolbarsView2");
//observing the data changes
refToolbars2.observe(DataEventType.value, with: { (snapshot) in
//if the reference have some values
if snapshot.childrenCount > 0 {
//clearing the list
self.toolbarList.removeAll()
//iterating through all the values
for toolbar in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
let toolbarObject = toolbar.value as? [String: AnyObject]
let toolbarId = toolbarObject?["id"]
let toolbarToolbar1 = toolbarObject?["toolbar1"]
//creating artist object with model and fetched values
//let toolbar = ToolbarModel(toolbar1: "String?")
//creating artist object with model and fetched values
let toolbar = View2ToolbarModel(id: toolbarId as! String?, toolbar1: toolbarToolbar1 as! String?)
//appending it to list
self.toolbarList.append(toolbar)
}
//reloading the tableview
self.tableViewToolbar.reloadData()
}
})
// scrollView.contentSize = CGSize(width: view.frame.width, height: view.frame.height + 83)
}
func addToolbar(){
//generating a new key inside artists node
//and also getting the generated key
let key = refToolbars2.childByAutoId().key
//creating artist with the given values
let toolbar = ["id":key,
"toolbar1": toolbar1.text! as String,
// "toolbar2": toolbar2.text! as String,
// "toolbar3": toolbar3.text! as String,
// "toolbar4": toolbar4.text! as String,
// "toolbar5": toolbar5.text! as String,
// "toolbar6": toolbar6.text! as String,
// "toolbar7": toolbar7.text! as String,
// "toolbar8": toolbar8.text! as String,
// "toolbar9": toolbar9.text! as String,
// "toolbar10": toolbar10.text! as String,
// "toolbar11": toolbar11.text! as String,
// "toolbar12": toolbar12.text! as String,
]
//adding the artist inside the generated unique key
refToolbars2.child(key).setValue(toolbar)
// refToolbars.child(key).setValue(toolbar1)
// refToolbars.child(key).setValue(toolbar2)
// refToolbars.child(key).setValue(toolbar3)
// refToolbars.child(key).setValue(toolbar4)
// refToolbars.child(key).setValue(toolbar5)
// refToolbars.child(key).setValue(toolbar6)
// refToolbars.child(key).setValue(toolbar7)
// refToolbars.child(key).setValue(toolbar8)
// refToolbars.child(key).setValue(toolbar9)
// refToolbars.child(key).setValue(toolbar10)
// refToolbars.child(key).setValue(toolbar11)
// refToolbars.child(key).setValue(toolbar12)
//
//displaying message
// labelMessage.text = "toolbar"
//https://www.simplifiedios.net/firebase-realtime-database-tutorial/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
// let segueName: String = segue.identifier!;
// if (segueName == "addButton") {
// let weeklyObjectivesViewController = segue.destination as! WeeklyObjectivesViewController;
//
//// weeklyObjectivesViewController.stringPassed1 = toolbar1.text
//
// }
// }
//
}
//
// AddNewObjective.swift
// Studioso
//
// Created by Charles Porth on 9/21/17.
// Copyright © 2017 Studioso. All rights reserved.
//
import Foundation
import QuartzCore
import UIKit
import FirebaseCore
import FirebaseDatabase
class AddNewObjectiveViewController: UIViewController {
#IBOutlet var text : UITextView?
#IBOutlet var textfield : UITextField!
var selectedKey = ""
var transferRow: String = ""
//defining firebase reference var
var refObjective: DatabaseReference!
//var textString: String!
//var textfieldString: String!
//https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/PersistData.html
#IBAction func Save(_ sender: Any) {
// self.updateToolbar(id: <#String#>)
addObjective()
// bodyReturn.body = self.text?.text
// bodyReturn.title = self.textfield.text
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//not perimantent
self.text?.layer.borderWidth = 1
self.text?.layer.borderColor = UIColor.red.cgColor
print(selectedKey)
refObjective = Database.database().reference().child("toolbar");
// toolbar.selectedKey = self.transferRow!
// refObjective = Database.database().reference().child("toolbars");
// let bodyFire = refObjective.child("-KwSYtZU_EX1vFFeTI4g").child("body")
print(self.transferRow)
// let bodyFire = self.refObjective.child(transferRow).child("body")
// bodyFire.observeSingleEvent(of: .value, with: { (snapshot) in
// let bodyOnFire = snapshot.value as? String
// self.text?.text = bodyOnFire
//
////
// let titleFire = self.refObjective.child("id").child("title")
// titleFire.observeSingleEvent(of: .value, with: { (snapshot) in
// let titleOnFire = snapshot.value as? String
// self.textfield.text = titleOnFire
//
// })
//This is the key that is desired.
// self.transferRow = WeeklyObjectivesEditorViewController.toolbarList[indexPath.row]
//
}
func addObjective(){
//generating a new key inside artists node
//and also getting the generated key
//let key = refObjective.childByAutoId().key
// let key = refObjective.child("-KwSYtZU_EX1vFFeTI4g").key
//creating artist with the given values
let objective = ["id": self.transferRow,
"body": text?.text! as! String,
"title": textfield.text! as String
] as [String : Any]
//adding the artist inside the generated unique key
refObjective.child(self.transferRow).setValue(objective)
//displaying message
}
// func updateToolbar(id:String){
// //creating artist with the new given values
// let objective = ["id":id,
// "body": text?.text! as! String,
// "title": textfield.text! as! String,
// ]
//
// //updating the artist using the key of the artist
// refObjective.child(id).setValue(objective)
//
// }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

How can I passing data UIViewContoller from UIView in swift3

How can i passing data uiviewController from uiview
I am Using function but it was not working
protocol name is startcalldelegate and function name is startcall
UIView Code
protocol StartCallDelegate: class {
func startCall(localNickname :String, remoteNickname :String)}
class CardView: UIView {
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
weak var delegate: CardViewDelegate?
weak var socketdelegate: StartCallDelegate?
#IBOutlet weak var UserPhoto: UIImageView!
#IBOutlet weak var UserNickName: UILabel!
#IBOutlet weak var UserAge: UILabel!
#IBOutlet weak var UserPeople: UILabel!
var localNickname: String = ""
var remoteNickname: String = ""
#IBAction func SendMessage(_ sender: Any) {
print("SendMessage")
//print(localNickName)
//print(UserNickName.text!)
}
#IBAction func SendVideoCall(_ sender: Any) {
print("SendVideoCall")
let entityDescription = NSEntityDescription.entity(forEntityName: "Profile", in: managedObjectContext)
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = entityDescription
do {
let objects = try managedObjectContext.fetch(request)
if objects.count > 0 {
let match = objects[0] as! NSManagedObject
localNickname = match.value(forKey: "nick") as! String
} else {
print("Nothing founded")
}
} catch {
print("error")
}
remoteNickname = UserNickName.text!
socketdelegate?.startCall(localNickname: localNickname, remoteNickname: remoteNickname)
delegate?.VideoChatSegue()
}
}
UIViewcontroller Code
class ViewController: UIViewcontroller, StartCallDelegate {
var localNickname: String = ""
var remoteNickname: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print(localNickname)
print(remoteNickname)
}
func startCall(localNickname: String, remoteNickname: String) {
print("Action startcall func")
self.localNickname = localNickname
self.remoteNickname = remoteNickname
}
startCall func not working
You need to define delegate in viewcontroller' ViewDidLoad
let objOardView = CardView() // this is only test purpose
objOardView.socketdelegate = self

Sort Firebase data by date

I keep getting this error and I failed to debug:
Could not cast value of type 'FIRDatabaseQuery' (0x10b32b700) to 'FIRDatabaseReference' (0x10b32b520).
That error comes from a regular .swift file with:
import Foundation
import Firebase
import FirebaseDatabase
let DB_BASE = FIRDatabase.database().reference()
class DataService {
static let ds = DataService()
private var _REF_BASE = DB_BASE
private var _REF_INCOMES = DB_BASE.child("incomes").queryOrdered(byChild: "date")
private var _REF_USERS = DB_BASE.child("users")
var REF_BASE: FIRDatabaseReference {
return _REF_BASE
}
var REF_INCOMES: FIRDatabaseReference {
return _REF_INCOMES as! FIRDatabaseReference // Thread 1: signal SIGABRT
}
[...]
}
Before adding .queryOrdered(byChild: "date") and as! FIRDatabaseReference everything worked except that I could not get a sort by date.
class IncomeFeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var incomes = [Income]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
DataService.ds.REF_INCOMES.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
if let incomeDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let income = Income(incomeId: key, incomeData: incomeDict)
self.incomes.append(income)
}
}
}
self.tableView.reloadData()
})
}
[...]
}
What am I after? To start, I need to sort my date then work towards my Sketch view:
How do you sort? Few tutorials I see uses CoreData. Im using Firebase.
your private var _REF_INCOMES is FIRDatabaseQuery not FIRDatabaseReference ..
var REF_INCOMES: FIRDatabaseQuery {
return _REF_INCOMES
}
And please check this Q&A to sort your array

Firebase Database not Uploading

I have my upload code here
import UIKit
import Firebase
class ChatViewController: UIViewController {
let chatRef = FIRDatabase.database().reference().child("chat")
let userUid = FIRAuth.auth()?.currentUser?.uid
var userName = ""
#IBOutlet weak var topBar: UINavigationItem!
#IBOutlet weak var containerView: UIView!
#IBOutlet var inputTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
topBar.title = "Chat Log Controller"
FIRDatabase.database().reference().child("users/\(userUid!)/name").observe(.value) { (snap: FIRDataSnapshot) in
self.userName = (snap.value! as! String).description
}
}
#IBAction func handleSend(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}
#IBAction func handleSendByEnter(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
print(userName)
childChatRef.child("name").setValue(userName)
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}
}
text is successfully uploaded But
It doesn't print userName and doesn't upload it to Firebase Database
But username is nut nil!
Try to use your observer code as,
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
}
Just take self.username = snap.value! as! String
It will solve your problem.

Resources