I'm trying to get access to the values stored in firebase dashboard to use them in different functions and methods in the class.
I used this method in this question
I have tried to print their values, the whole app has crashed and it gave me that their nil!
They are not nil actually!
I used a similar method in viewDidLoad and I could retrieve the values to labels!
let refer = FIRDatabase.database().reference().child("UserDevices")
var globalEmail : String!
var globalPhone : String!
var globalImageUrl: String!
override func viewWillAppear(_ animated : Bool){
super.viewWillAppear(animated)
retrieveUserData{(email,phone,ImageUrl) in
self.globalEmail = email
self.globalPhone = phone
self.globalImageUrl = ImageUrl
}
}
func retrieveUserData(_ completionBlock : #escaping ((_ email : String?, _ phone : String?, _ ImageUrl: String?)->Void)){
refer.child(byAppendingPath: self.strUserid as String).observe(.value , with: {snapshot in
if let userDict = snapshot.value as? [String:AnyObject] {
completionBlock(userDict["email"] as! String, userDict["phone"] as! String, userDict["ImageUrl"] as! String)
}
})
}
var strUserid : NSString!
override func viewDidLoad() {
super.viewDidLoad()
print(globalEmail)
print(globalImageUrl)
print(globalPhone)
self.navigationController?.navigationBar.tintColor = UIColor.white
print("idis \(self.strUserid)")
let ref = FIRDatabase.database().reference().child("UserDevices")
self.navigationController?.navigationBar.tintColor = UIColor.white
ref.child(byAppendingPath: self.strUserid as String).observe(.value, with: { snapshot in
if let dict = snapshot.value as? NSMutableDictionary{
print("dict is \(dict)")
if let Provider = dict["name"] as? String
{
self.DeviceDetailsProvider.text = Provider
// self.navigationItem.title = Provider
}
if let name = dict["DeviceName"] as? String
{
self.DeviceDetailsName.text = name
self.navigationItem.title = name
}
if let ShortDescription = dict["Description"] as? String
{
self.DeviceDetailsDescription.text = ShortDescription
}
if let City = dict["city"] as? String
{
self.DeviceDetailsCity.text = City
}
}
})
self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl)
}
Why I'm getting a crash here!
Change ref.child(byAppendingPath: self.strUserid as String)
To:-
ref.child(self.strUserid)
Also remove let refer = FIRDatabase.database().reference().child("UserDevices").
You can not initialise your reference globally outside a scope because you don't know in which order your classes are being initialised, and probable scenario is that your FIRDatabase hasn't even been initialised yet when you try to initialise let refer.
Instead of refer in retrieveUserData use
FIRDatabase.database().reference().child("UserDevices")
You see in a viewController's LIFE CYCLE, viewdidLoad is called before than viewWillAppear:
So what you need is:-
override func viewDidLoad() {
super.viewDidLoad()
..
retrieveUserData{(email,phone,ImageUrl) in
self.globalEmail = email
self.globalPhone = phone
self.globalImageUrl = ImageUrl
self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl)
// .. Do your stuff...
}
}
Read: Viewcontroller's Lifecycle
Related
I know this has been asked on stack overflow several times but I cannot seem to find the answer I am looking for. I am trying to store data from a firebase database (using the observeSingleEvent(snapshot)) method in a global variable. See below code for details.
I've tried adding a completion handler and followed steps online, but doing so, the observeSingleEvent request stops working.
What I had before:
class ItemsTableViewController: UITableViewController {
let listToUsers = "ListToUsers"
var user: User!
let ref = Database.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
let defaultUser = User()
ref.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
defaultUser.uid = uid
defaultUser.email = value?["email"] as? String ?? ""
defaultUser.name = value?["name"] as? String ?? ""
defaultUser.grad = value?["grad"] as? Int ?? 0
defaultUser.number = value?["number"] as? String ?? ""
defaultUser.image = UIImage(named: value?["image"] as? String ?? "")!
completion(defaultUser)
}) { (error) in
print("hello")
print(error.localizedDescription)
}
self.user = defaultUser
}
}
What I tried after and still did not work:
class ItemsTableViewController: UITableViewController {
// MARK: Constants
let listToUsers = "ListToUsers"
// MARK: Properties
var user: User!
let ref = Database.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
let use = Auth.auth().currentUser?.uid
self.getUserData(uid:use!) { (user) -> () in
self.user = user
}
}
func getUserData(uid:String , completion: #escaping (User) -> ()) {
ref.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let defaultUser = User()
defaultUser.uid = uid
defaultUser.email = value?["email"] as? String ?? ""
defaultUser.name = value?["name"] as? String ?? ""
defaultUser.grad = value?["grad"] as? Int ?? 0
defaultUser.number = value?["number"] as? String ?? ""
defaultUser.image = UIImage(named: value?["image"] as? String ?? "")!
completion(defaultUser)
}){ (error) in
print("hello")
print(error.localizedDescription)
}
}
Firebase is asynchronous and the self.user = defaultUser in the first code block is going to be called way before Firebase has a chance to return data.
Data is ONLY valid within the closure following the Firebase call. So here's how to fix it. Oh, and remove that error code as it isn't needed.
Assume a user structure like this
users
uid_0
name: "some name"
email: "some email"
and the code to read in uid_0 and populate a global (i.e. class var) user
class MyUser {
var uid = ""
var name = ""
var email = ""
init(aUid: String, aName: String, aEmail: String) {
self.uid = aUid
self.name = aName
self.email = aEmail
}
}
var user: MyUser!
func readOneUser() {
let uid = "uid_0"
let thisUserRef = self.ref.child("users").child(uid)
thisUserRef.observeSingleEvent(of: .value, with: { snapshot in
//let uid = snapshot.key if you don't know the uid, it will be the key to the node
let name = snapshot.childSnapshot(forPath: "name").value as? String ?? "No Name"
let email = snapshot.childSnapshot(forPath: "email").value as? String ?? "No Email"
self.user = MyUser(aUid: uid, aName: name, aEmail: email)
print(self.user.name) //do something with the user here
})
}
and this line
defaultUser.image = UIImage(named: value?["image"] as? String ?? "")!
needs to be addressed but I don't know what the intention is. It could end up being nil if this happens
defaultUser.image = UIImage(named: "")!
so you may want to populate it with a default image of some kind or provide error handling in case it is nil.
In order to populate my tableView, I append items (created from a struct) to a local array:
func loadList() {
var newAnnotations: [AnnotationListItem] = []
if let uid = Auth.auth().currentUser?.uid {
uidRef.child(uid).child("annotations").queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in
for item in snapshot.children {
let annotationItem = AnnotationListItem(snapshot: item as! DataSnapshot)
newAnnotations.append(annotationItem)
}
annotationList = newAnnotations
self.tableView.reloadSections([0], with: .fade)
})
}
}
When I click a specific row, I am taken to a DetailViewController where it is only a large UITextView (named notes). The UITextView.text displayed is based on the selected indexPath.row and the "notes" value is retrieved from the array. Now the user is able to type some text and when they are done, the textViewDidEndEditing function is called:
func textViewDidEndEditing(_ textView: UITextView) {
notes.resignFirstResponder()
navigationItem.rightBarButtonItem = nil
let newNotes = self.notes.text
print(newNotes!)
}
Now I'd like to updateChildValues to newNotes to the child node "notes" in my JSON:
"users" : {
"gI5dKGOX7NZ5UBqeTdtu30Ze9wG3" : {
"annotations" : {
"-KuWIRBARv7osWr3XDZz" : {
"annotationSubtitle" : "1 Cupertino CA",
"annotationTitle" : "Apple Infinite Loop",
"notes" : "Does it work?!",
}
How can I access the selected autoID so I can update the specific notes node. So far the best I have is:
guard let uid = Auth.auth().currentUser?.uid else { return }
uidRef.child(uid).child("annotations").(somehow access the specific childID).updateChildValues(["notes": newNotes])
Any help will be greatly appreciated. Thanks in advance
UPDATE
The annotationListItem struct is created:
struct AnnotationListItem {
let key: String?
var annotationTitle: String?
let annotationSubtitle: String?
let notes: String?
let ref: DatabaseReference?
init(key: String = "", annotationTitle: String, annotationSubtitle: String, notes: String) {
self.key = key
self.annotationTitle = annotationTitle
self.annotationSubtitle = annotationSubtitle
self.notes = notes
self.ref = nil
}
init(snapshot: DataSnapshot) {
key = snapshot.key
let snapshotValue = snapshot.value as! [String: AnyObject]
annotationTitle = snapshotValue["annotationTitle"] as? String
annotationSubtitle = snapshotValue["annotationSubtitle"] as? String
notes = snapshotValue["notes"] as? String
ref = snapshot.ref
}
init(Dictionary: [String: AnyObject]) {
self.key = Dictionary["key"] as? String
self.annotationTitle = Dictionary["annotationTitle"] as? String
self.annotationSubtitle = Dictionary["annotationSubtitle"] as? String
self.notes = Dictionary["notes"] as? String
self.ref = nil
}
func toAnyObject() -> Any {
return [
"annotationTitle": annotationTitle as Any,
"annotationSubtitle": annotationSubtitle as Any,
"notes": notes as Any
]
}
}
UPDATE
This is how the annotationListItem is created to be stored in Firebase:
// Using the current user’s data, create a new AnnotationListItem that is not completed by default
let uid = Auth.auth().currentUser?.uid
guard let email = Auth.auth().currentUser?.email else { return }
let title = placemark.name
let subtitle = annotation.subtitle
let notes = ""
// declare variables
let annotationListItem = AnnotationListItem(
annotationTitle: title!,
annotationSubtitle: subtitle!,
notes: notes)
// Add the annotation under their UID
let userAnnotationItemRef = uidRef.child(uid!).child("annotations").childByAutoId()
userAnnotationItemRef.setValue(annotationListItem.toAnyObject())
I think you only need to do this:(since you have declared the note as global)
guard let uid = Auth.auth().currentUser?.uid else { return }
uidRef.child(uid).child("annotations").(note.key).updateChildValues(["notes": newNotes])
inside the method where you change the notes
If I am not mistaken you are creating an array of a custom object?
var newAnnotations: [AnnotationListItem] = []
You could do something like: var newAnnotations: [(key: String, value: [String : Any])] = [] (Any only if you are going to have Strings, Integers, ect. If it'll only be String then specify it as a String.
Accessing the key would be: newAnnotations[indexPath.row].key in your cellForRowAtIndex of your tableView. Accessing values would be: newAnnotations[indexPath.row].value["NAME"].
You can have a separate array that holds the key and just append it at the same time as your population:
for item in snapshot.children {
guard let itemSnapshot = task as? FDataSnapshot else {
continue
}
let id = task.key //This is the ID
let annotationItem = AnnotationListItem(snapshot: item as! DataSnapshot)
newAnnotations.append(annotationItem)
}
Another thing you could do is go up one more level in your firebase call:
if let uid = Auth.auth().currentUser?.uid {
uidRef.child(uid).child("annotations").observeSingleEvent(of: .value, with: {snapshot in
if snapshot is NSNull{
//Handles error
} else{
if let value = snapshot.value as? NSDictionary{ //(or [String: String]
//set localDictionary equal to value
}
}
self.tableView.reloadSections([0], with: .fade)
})
}
And then when you select a row: let selectedItem = localDictionary.allKeys[indexPath.row] as! String //This is the ID you pass to your viewController.
I am trying to display the user data in screen. But I always get an empty value. I don't know why.
var profileData = Profile(usrObj: [String:String]())
#IBOutlet weak var userName: UILabel!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
self.userName.text = profileData.FirstName
print(profileData.FirstName)
}
My print statement and my label value are empty. Please help me out with any mistake I am making.
My model class :
class Profile {
var FirstName: String
init(usrObj : [String: AnyObject]) {
self.FirstName = (usrObj["FirstName"] ?? "") as! String
}
var ProfileObject: [String:AnyObject] {
return ["FirstName" : self.FirstName]
}
In your LoginViewController save your data in NSUserDefaults
#IBAction func loginWithUserNamePassword(){
KRProgressHUD.show(progressHUDStyle: .White, message: "Loading...")
loginWithMailAndPassword((username.text?.trimWhiteSpace)!, password: (password.text?.trimWhiteSpace)!) { (user, error) in
if error != nil{
KRProgressHUD.dismiss()
SCLAlertView().showError("Login Error", subTitle: error!.localizedDescription)
}
else {
if user!.emailVerified
{
currentUser = user
fireBaseRef.child("Users").child(currentUser!.uid).child("UserProfile").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if let data: [String : AnyObject] = snapshot.value as? [String : AnyObject] {
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(data, forKey: "userdata")
userDefaults.synchronize()
enableSync()
self.navigateToNextScreen()
}
else{
}
})
}
else
{
SCLAlertView().showError("Login Error", subTitle: "This email is has not been verified yet")
}
}
}
}
and use that data in UserStaticDataViewController
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
self.profileDetailsExists = true
let userdata : NSDictionary = NSUserDefaults.standardUserDefaults().valueForKey("userdata") as! NSDictionary
print(userdata["City"] as? String)
self.userName.text = userdata["FirstName"] as? String
self.userCity!.text = userdata["City"] as? String
self.userCountry!.text = userdata.valueForKey("Country") as? String
self.userState.text = userdata.valueForKey("State") as? String
self.userMobileNo.text = userdata.valueForKey("Mobile") as? String
self.userGmail.text = userdata.valueForKey("Email") as? String
self.userDob.text = userdata.valueForKey("DateOfBirth") as? String
}
Output:
Just approve my answer and give vote.
Happy coding.
From the fact that your print statement is empty, it means that the userObject you pass into the following equation
var profileData = Profile(usrObj: [String:String]())
probably does not have a string value for the key "firstName".
You can try to verify this by changing your code
self.FirstName = (usrObj["FirstName"] ?? "") as! String
to
self.FirstName = (usrObj["FirstName"] ?? "Hello world") as! String
and see if "Hello world" is printed out in your console.
If yes, then you just have to make sure that the "user" object you pass into the init function of Profile class should be a dictionary where a value is stored for the key "FirstName"
So I'm saving a dictionary to my firebase. The dictionary is located in a custom class I made called FoodItem. Here's the lines where I save the dictionary:
let favRef = self.ref.childByAppendingPath("urlhidden")
favRef.setValue(foodItem.toAnyObject())
foodItem is a FoodItem object, here's the part of my FoodItem class that implements .toAnyObject()
var key: String
var ref: Firebase?
var name: String
var description: String
var minCal: Int
var maxCal: Int
var containsNuts: Bool
var vegetarian: Bool
var price: Double //**NOTE THAT PRICE IS A DOUBLE**
func toAnyObject()->[String:AnyObject]{
return ["name":self.name,
"price":self.price,
"description":self.description,
"minCal":self.minCal,
"maxCal":self.maxCal,
"containsNuts":self.containsNuts,
"vegetarian":self.vegetarian]
}
So those bits of code combined saves my data to my firebase. Works as it should. But when I retrieve the data, my price variable is a string? I'll explain...
Here's where I retrieve the data:
func getCurrentOrder(){
let uid = ref.authData.uid
ref = Firebase(url: "urlhidden")
ref.observeEventType(.Value, withBlock: { snapshot in
var newItems = [FoodItem]()
for item in snapshot.children {
let foodItem = FoodItem(snapshot: item as! FDataSnapshot)
newItems.append(foodItem)
}
self.order = newItems
self.tableView.reloadData()
})
}
That FoodItem constructor you see with the snapshot takes the data from my firebase and assigns my FoodItem variables to those values in my firebase. Here's that code:
init(snapshot: FDataSnapshot) {
key = snapshot.key
name = snapshot.value["name"] as! String
price = snapshot.value["price"] as! Double //**NOTE STILL A DOUBLE**
description = snapshot.value["description"] as! String
minCal = snapshot.value["minCal"] as! Int
maxCal = snapshot.value["maxCal"] as! Int
containsNuts = snapshot.value["containsNuts"] as! Bool
vegetarian = snapshot.value["vegetarian"] as! Bool
ref = snapshot.ref
}
However when I run my code, I see that it prints the double as a String
As you can see, there are clearly quotes around the Double variable "price".
And I get this error:
Could not cast value of type '__NSCFString' (0xff0ee0) to 'NSNumber' (0x13ff81c).
With this line of code highlighted in red:
price = snapshot.value["price"] as! Double
Which is located inside the FoodItem constructor with the snapshot as I posted above.
Any Ideas???
*EDIT: It does the same thing for my int variables. Same error message
It never actually saved it as a String. I believe I had that value in there as some old code in which I did save it as a String. I deleted my entries in the database and tried it again and it works just fine. Thank you to those who attempted to answer my question.
2017 example
It may save someone some typing, here's some completely typical code to observe a simple number value. Let's say it's a fraction called SomeFraction.
As always, Firebase rocks :)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
dev_listenForLiveFraction()
}
var r1: DatabaseReference? = nil
func dev_listenForLiveFraction() {
let p1 = "something/someFractionForExample"
r1 = Database.database().reference(withPath: p1)
r1!.observe(.value) { (snapshot) in
let sf: NSNumber = snapshot.value as? NSNumber ?? 0.666
self.example_handleSomeFraction(sf)
}
}
func example_handleSomeFraction(_ sf: NSNumber) {
print("the fraction is now \(sf)")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
clearObservations()
}
private func clearObservations() {
if r1 != nil {
r1?.removeAllObservers()
r1 = nil
}
}
I'm having a modal entity file as below,
import UIKit
class MyProfile: NSObject {
var userName : String = ""
func initWithDict(dict: NSMutableDictionary) {
self.userName = dict.objectForKey("username") as! String
}
}
Saving that entity by encoding as below,
let myDict: NSMutableDictionary = ["username": "abc"]
let myEntity:MyProfile = MyProfile()
myEntity.initWithDict(myDict)
let userDefaults = NSUserDefaults.standardUserDefaults()
let encodedData = NSKeyedArchiver.archivedDataWithRootObject(myEntity)
userDefaults.setObject(encodedData, forKey: "MyProfileEntity")
userDefaults.synchronize()
Getting that saved entity as below,
let myEntity:MyProfile = MyProfile()
let userDefaults = NSUserDefaults.standardUserDefaults()
guard let decodedNSData = userDefaults.objectForKey("MyProfileEntity") as? NSData,
myEntity = NSKeyedUnarchiver.unarchiveObjectWithData(decodedNSData) as? MyProfile!
else {
print("Failed")
return
}
print(myEntity.userName)
It's not working, having crashes and lot of syntax errors, I'm new to swift,
It's showing some syntax errors like definition conflicts with previous value in the unarchiveObjectWithData line. If I fix that error, then at the time of getting the entity from userdefaults it's crashing.
can anyone suggest how can I resolve it?
To save custom object into user default, you must implement NSCoding protocol. Please replace your custom data model like this:
class MyProfile: NSObject,NSCoding {
var userName : String = ""
#objc required init(coder aDecoder:NSCoder){
self.userName = aDecoder.decodeObjectForKey("USER_NAME") as! String
}
#objc func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.userName, forKey: "USER_NAME")
}
init(dict: [String: String]) {
self.userName = dict["username"]!
}
override init() {
super.init()
}
}
Here is the code for saving and retrieving MyProfile object:
// Save profile
func saveProfile(profile: MyProfile){
let filename = NSHomeDirectory().stringByAppendingString("/Documents/profile.bin")
let data = NSKeyedArchiver.archivedDataWithRootObject(profile)
data.writeToFile(filename, atomically: true)
}
// Get profile
func getProfile() -> MyProfile?{
if let data = NSData(contentsOfFile: NSHomeDirectory().stringByAppendingString("/Documents/profile.bin")){
let unarchiveProfile = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! MyProfile
return unarchiveProfile
} else{
return nil
}
}
Now here is the code snippet how to use those method:
// Create profile object
let profile = MyProfile(dict: ["username" : "MOHAMMAD"])
// save profile
saveProfile(profile)
// retrieve profile
if let myProfile = getProfile(){
print(myProfile.userName)
}else{
print("Profile not found")
}
You can't do this:
let myEntity:MyProfile = MyProfile()
Then later on, do this:
myEntity = ...
When something is defined with 'let', you cannot change it.
Change to
var myEntity: MyProfile?
It is possible that
NSKeyedUnarchiver.unarchiveObjectWithData(decodedNSData)
is returning nil. You then proceed to force unwrapping by adding
as? MyProfile!
try changing this to
as? MyProfile
Then later, see if you got something back
if let myEntity = myEntity {
print(myEntity.userName)
}