I am saving data in swift, to a realtime firebase database using the following code:
guard let key = dbreference.child(("user-clients")).childByAutoId().key else { return }
let post = ["uid": userID,
"firstname": clientfirstname,
"lastname": clientlastname,
"dateofbirth": clientdateofbirth,
"haircolour": clienthaircolour,
"products": clientproduct]
let childUpdates = ["/user-clients/\(String(describing: user))/\(key)/": post]
dbreference.updateChildValues(childUpdates)
This automatically creates a child ID under the user that is logged in (so is my understanding?) and it works fine as my database is updating correctly.
The problem is - I am trying to read the first name and last name of each person and display in a table view (like a contact card) but I cannot 'pull out' the firstname and lastname data.
Here is my code to retrieve the data:
let dbref = Database.database().reference()
let me: String = (Auth.auth().currentUser?.uid)!
let key = dbreference.child(("users")).childByAutoId().key
//Access the databse and retrieve First and Last Names from child
dbref.child("user-clients").queryOrdered(byChild: me).observeSingleEvent(of: DataEventType.value) { (snapshot) in
if snapshot.exists() {
print("exists")
for child in snapshot.children {
let data = child as! DataSnapshot
//print(data.key)
print(data.value!)
if (data as AnyObject).hasChild("firstname") {
print("perfect")
} else {
print("does not exist")
}
if let firstname = snapshot.childSnapshot(forPath:"firstname").value as? String {
print(firstname)
}
if let lastname = snapshot.childSnapshot(forPath:"lastname").value as? String {
print(lastname)
}
}
}
It seems like I am not getting down another level as the console prints out all the contacts under the different autogenerated ID's. My JSON file looks like this:
{
"user-clients" : {
"Optional(\"4WNCGOaiIMTKAa12Pmi26w11hSb2\")" : {
"-M943knUA5JkpMuxygti" : {
"dateofbirth" : "7777",
"firstname" : "john",
"haircolour" : "blue",
"lastname" : "melon",
"products" : "09876543321",
"uid" : "4WNCGOaiIMTKAa12Pmi26w11hSb2"
},
"-M944HmLvM50rEc4qtM3" : {
"dateofbirth" : "12/12/12",
"firstname" : "basil",
"haircolour" : "yellow",
"lastname" : "brush",
"products" : "678990099",
"uid" : "4WNCGOaiIMTKAa12Pmi26w11hSb2"
},
"-M94C9yk7q6mQglQiaoR" : {
"dateofbirth" : "12/12/77",
"firstname" : "Homer",
"haircolour" : "yellow",
"lastname" : "Simpson",
"products" : "123456789",
"uid" : "4WNCGOaiIMTKAa12Pmi26w11hSb2"
},
"-M94CISM865qWAa8nbK-" : {
"dateofbirth" : "12/11/77",
"firstname" : "Marge",
"haircolour" : "Blue",
"lastname" : "Simpson",
"products" : "09876654433",
"uid" : "4WNCGOaiIMTKAa12Pmi26w11hSb2"
}
Any help would be greatly appreciated and I hope the above makes sense. Thanks in advance.
Related
This just isn't returning anything even though there are messages with timestamps above this one.
Code not properly sorting:
let messagesRef = FIRDatabase.database().reference().child("all-messages").child(messageId).queryOrdered(byChild: "timestamp").queryStarting(atValue: 1511130644)
Full code of Firebase calls:
FIRDatabase.database().reference().child("users").child(currentUserUid).child("timestampOfLastVisit").observeSingleEvent(of: .value, with: { (snapshot) in
timestamp = snapshot.value as! NSNumber
groupMessagesRef = FIRDatabase.database().reference().child("groups").child(groupId).child("messages")
groupMessagesRef.observe(.childAdded, with: { (snapshot) in
if self.sentMessage {
let messageId = snapshot.key
let messagesRef = FIRDatabase.database().reference().child("all-messages").child(messageId).queryOrdered(byChild: "timestamp").queryStarting(atValue: 1511130644)
messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in ...
Relevant Firebase JSON (fake data obviously):
{
"all-messages" : {
"-KzLOuvEdLWy7vfc2XsT" : {
"fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
"groupId" : "-Kxk3kA9I8OEvNmFcavL",
"isStarred" : true,
"text" : "This should show",
"timestamp" : 1511130640
},
"-KzLOw5OxZ4P_I0d70gZ" : {
"fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
"groupId" : "-Kxk3kA9I8OEvNmFcavL",
"isStarred" : false,
"text" : "And so should this",
"timestamp" : 1511130644
},
"-KzLS4uwDtpW6wHNxrmc" : {
"fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
"groupId" : "-Kxk3kA9I8OEvNmFcavL",
"isStarred" : false,
"text" : "1",
"timestamp" : 1511131471
},
"-KzLS5FvASnj_ky4WQkd" : {
"fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
"groupId" : "-Kxk3kA9I8OEvNmFcavL",
"isStarred" : false,
"text" : "2",
"timestamp" : 1511131472
}
},
"groups" : {
"-Kxk3kA9I8OEvNmFcavL" : {
"groupCreator" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
"groupDescription" : "Group",
"groupImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/group_profile_images%2FA4DDA286-E7A3-4F96-ABF0-5717F4029C33.png?alt=media&token=6ad79728-74fd-405f-abbf-247dae4684b5",
"groupMemberOneSignalIds" : {
"5905a2f5-dfa3-46d7-964f-596bd2f2004a" : true,
"67b9cb2a-6583-45b4-9fed-93cf333e9ca4" : true,
"a1bf9ed9-0959-4c88-974b-5a829c1cdcc9" : true,
"f2fb84b9-86af-40e7-8667-989a95b2e282" : true
},
"groupMembers" : {
"3lQiuzudFGW51UwQ4Mseu8aOxyu2" : true,
"JF7SCe3VUcWJi1ujumx0VpyaG5s1" : true,
"YEbVehx0cqTvNaKrJTcs5GnRLGM2" : true,
"zhBZyAVGvAMZWi4QhvKDu7qb3Qr1" : true
},
"groupName" : "1",
"groupTags" : {
"-Kxk3kHxMuWQHEsaWN5k" : "1",
"-Kxk3kHyMB2OL5pNAfW1" : "1",
"-Kxk3kHyMB2OL5pNAfW2" : "1"
},
"messages" : {
"-KzLOuvEdLWy7vfc2XsT" : 1,
"-KzLOw5OxZ4P_I0d70gZ" : 1,
"-KzLS4uwDtpW6wHNxrmc" : 1,
"-KzLS5FvASnj_ky4WQkd" : 1
},
"starredMessages" : {
"-KzLOuvEdLWy7vfc2XsT" : 1511204335
}
},
"-Kxn_yTAZvkPls-_s8He" : {
"groupCreator" : "JF7SCe3VUcWJi1ujumx0VpyaG5s1",
"groupDescription" : "Group 3",
"groupImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/group_profile_images%2FD2C68A8F-33DC-43C6-8C99-762315760208.png?alt=media&token=aae87339-877e-484e-a0e1-a2e13455176c",
"groupName" : "Group 3",
"groupTags" : {
"-Kxn_ygovR7mQIph3WiC" : "1",
"-Kxn_ygovR7mQIph3WiD" : "1"
}
}
},
"users" : {
"JF7SCe3VUcWJi1ujumx0VpyaG5s1" : {
"email" : "Madi#gmail.com",
"groups" : {
"-Kxk3kA9I8OEvNmFcavL" : true,
"-Kxk3n99i43MYECm1Ix8" : true,
"-KxuBPAPBIcROMEtNujW" : true,
"-KxuET1pZGg2x_aJvRkU" : true
},
"profileImageURL" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/profile_images%2F246CA55C-446D-4A24-8BE6-8B5B9C0F27AF.png?alt=media&token=47022ccd-1c92-460a-8630-e74a0d68bc4a",
"searchUsername" : "madi",
"timestampOfLastVisit" : 1511241228,
"username" : "Madi"
},
"zhBZyAVGvAMZWi4QhvKDu7qb3Qr1" : {
"email" : "Connor#gmail.com",
"groups" : {
"-Kxk3kA9I8OEvNmFcavL" : true,
"-Kxk3n99i43MYECm1Ix8" : true
},
"profileImageURL" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/profile_images%2FBDC4020E-9F54-4F80-A4DC-668804215DE3.png?alt=media&token=ec2c8875-e6e4-40d5-8c07-3d743bf4ea16",
"searchUsername" : "connor",
"timestampOfLastVisit" : 1511205356,
"username" : "Connor"
}
}
}
What print(snapshot) prints:
Snap (-KzLOw5OxZ4P_I0d70gZ) <null>
print(messageRef):
(/all-messages/-KzLOw5OxZ4P_I0d70gZ {
i = timestamp;
sp = 1511130644;
})
The goal is to create a query that will return the node that has a particular time stamp.
The problem is your code is querying one level too deep. In other words, you need to let Firebase iterate over the child nodes of all-messages until it finds one that has a child timestamp of 1511130644. So it will need to iterate over
msg_0
msg_1
msg_2
etc.
Your code is trying to be too specific and you are telling it to look at a particular message Id for the data in the query - which doesn't make sense as if you know the exact path, you wouldn't need to query!
This is a common misunderstanding - just remember that queries need to be provided the parent node and then the child node of what you are querying for as it will then iterate over the child_nodes just underneath the parent.
parent_node
child_node
child_node_of_what_you_are_querying
child_node
child_node_of_what_you_are_querying
Here is code to query for a certain time stamp - note the messageId is not needed
//self.ref is the firebase ref
let messagesRef = self.ref.child("all-messages").queryOrdered(byChild: "timestamp")
.queryStarting(atValue: 1511130644)
messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChildren() {
print("got snap with children")
} else {
print("no snap found")
}
})
If you already know the specific path you can observe it directly. So if you want to print the timestamp for a certain message
let thisMsgRef = self.ref.child("all-messages").child(messageId)
let thisTimestampRef = thisMsgRef.child("timestamp")
thisTimestampRef.observeSingleEvent(of: .value) { snapshot in
print(snap.value) //prints the timestamp
}
The second argument to queryStartingAtValue() is only used to disambiguate between items that match the first argument. To filter by timestamp you should call queryOrderedByChild("timestamp"):
let messagesRef =
FIRDatabase.database().reference().child("all-messages").child(messageId)
.queryOrdered(byChild: "timestamp")
.queryStarting(atValue: 1511130644)
I'm trying to loop through children in my Firebase Database to retrieve a nested key.
My database is structured like this:
"Users" : {
"Username" : {
"Favorites" : {
"Location" : {
"Latitude" : 123,
"LocationName" : "San Francisco",
"Longitude" : 123
},
"Location2" : {
"Latitude" : 123,
"LocationName" : "London",
"Longitude" : 123
}
}
}
}
I am trying to print out all of the "LocationName" keys, and am able to print one instance of this key, but not able to loop through and print all instances of this key.
I'm not sure where in my for loop I'm going wrong?
The code I'm working with is below.
FIRApp.configure()
let databaseRef = FIRDatabase.database().reference().child("Users").child("Username").child("Favorites")
let databaseHandle = databaseRef.observe(.value, with: { (snapshot) in
for item in snapshot.children {
if let dbLocation = snapshot.childSnapshot(forPath: "LocationName") as? String {
print (dbLocation)
}
print(item)
}
})
I'm very new to Swift, and even newer to Firebase, so any help would be greatly appreciated!!
The problem in your code is that snapshot refers to the Favorites node – instead of looking for LocationName there, you should look for it inside each of the Location child nodes. Therefore your loop should look something like this:
let databaseHandle = databaseRef.observe(.value, with: { snapshot in
for child in snapshot.children {
let childSnapshot = snapshot.childSnapshotForPath(child.key)
if let dbLocation = childSnapshot.value["LocationName"] as? String {
print(dbLocation)
}
}
})
`here I am trying to get the highest pain level of each day of a month and save it in an array and plot it as a chart.I managed to get the pain level, but it retrieves only entered date alone .. I need to check date 1 to 31 of a month ...if its a nonentried day means..it should return 0 to the array.....any help will be appreciated..?
my code`
func DataFromFirebase()
{
var u_id = FIRAuth.auth()?.currentUser!.uid
print(u_id)
databaseRef = FIRDatabase.database().reference().child("medical_details")
databaseRef.queryOrderedByChild("user_id").queryEqualToValue(u_id).observeEventType(.Value, withBlock: { (snapshot) in
for the item in snapshot.children
{
let c_date = item.value!!["created_date"] as! String
let Key = item.key
//reference to the selected key
let usersRef = self.databaseRef.childByAppendingPath(Key!!)
//reference to the medicine node inside the key
let Ref = usersRef.childByAppendingPath("parts_list")
Ref.observeEventType(.Value , withBlock: { (snapshot) in
for the item in snapshot.children
{
let date = item.value!!["date"] as! String
if (c_date == date)
{
let dosage = item.value!!["pain_level"] as! String
print(dosage)
}
}
})
}
})
}
here my Firebase DB Structure
"-KfBHEzFkEpfy0g2sU4S" : {
"created_date" : "14/2/2017",
"created_time" : "15:36",
"medicine" : {
"-KfBHEzYDrMII9o2OECQ" : {
"dosage" : "2mg",
"frequency" : "8",
"interval" : "10",
"number_of_tablets" : "2",
"reminder" : "",
"tablet" : "Dilaudid"
}
},
"parts_list" : {
"-KfBHEzbFQEBEjFk5uLN" : {
"comments" : "cgsg",
"date" : "14/2/2017",
"pain_level" : "3",
"part_name" : "Leg",
"time" : "15:36"
},
"-KfBHEzeY45XKs-u8S9H" : {
"comments" : "vhdh",
"date" : "14/2/2017",
"pain_level" : "8",
"part_name" : "Mouth",
"time" : "15:36"
}
},
"remedies" : "Heating Pad",
"user_id" : "xqgiYomblxcFbSnO0Q9T5fFOKWL2"
},
need to retrieve that pain level according to all days of a month..?
I am reading JSON from two places 1) from a file and 2) from url.
this is my JSON file
{
"People": {
"Person1" : {
"Name" : "Umair",
"Age" : "22"
},
"Person2" : {
"Name" : "Rehman",
"Age" : "28"
},
"Person3" : {
"Name" : "Saqib",
"Age" : "32"
},
"Person4" : {
"Name" : "Fahad",
"Age" : "18 "
},
}
}
and this is how I read it using SwiftyJson
let path: String = NSBundle.mainBundle().pathForResource("jsonFile", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: path) as NSData!
let readableJSON = JSON(data: jsonData, options: .MutableContainers, error: nil)
var myName = readableJSON["People","Person1","Name"]
print(myName)
in result it gives me the name no problem here. when I try to get the Json from url using same method I get the complete Json file in return but when I try to get a specific field it return null. here is my code for that
let url = NSURL(string: "http://api.randomuser.me/")
let session = NSURLSession.sharedSession()
session.dataTaskWithURL(url!, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
// Make sure we get an OK response
guard let realResponse = response as? NSHTTPURLResponse where
realResponse.statusCode == 200 else {
print("Not a 200 response")
return
}
// Read the JSON
let readableJSONFromWeb = JSON(data: data!, options: .MutableContainers, error: nil)
var origin = readableJSONFromWeb["results"]
print(origin)
}).resume()
}
as per above code I get this output
Umair
[
{
"id" : {
"name" : "SSN",
"value" : "026-36-3780"
},
"nat" : "US",
"cell" : "(862)-296-4803",
"phone" : "(528)-560-7652",
"login" : {
"username" : "ticklishmouse295",
"password" : "julie",
"sha256" : "01234103cae3a10c5813a7c5dfd069e2533860cf2786df4dc31a501eb66b9c37",
"sha1" : "46fa91a26c5219823b0c4a2c0fd99d390c997cd9",
"salt" : "aU4VTMOV",
"md5" : "be3f89edd4f22f333433d2424cbf4a95"
},
"registered" : 1144149087,
"dob" : 1170844810,
"picture" : {
"large" : "https:\/\/randomuser.me\/api\/portraits\/women\/40.jpg",
"thumbnail" : "https:\/\/randomuser.me\/api\/portraits\/thumb\/women\/40.jpg",
"medium" : "https:\/\/randomuser.me\/api\/portraits\/med\/women\/40.jpg"
},
"location" : {
"state" : "north carolina",
"street" : "9402 mcclellan rd",
"city" : "arvada",
"postcode" : 93836
},
"email" : "ethel.palmer#example.com",
"gender" : "female",
"name" : {
"title" : "mrs",
"first" : "ethel",
"last" : "palmer"
}
}
]
but when I try to get a specific field using
var origin = readableJSONFromWeb["results","location"]
I get null in return
Subscripting like this with SwiftyJSON:
var origin = readableJSONFromWeb["results","location"]
only works if the entire path is made of dictionaries.
But your results key holds an array, so you have to pass the index of the array item you want to get.
Example for getting the first item from the array:
var origin = readableJSONFromWeb["results",0,"location"]
Result:
{
"city" : "sittard-geleen",
"postcode" : 85004,
"street" : "4774 pieterskerkhof",
"state" : "flevoland"
}
I have a problem, I'm trying to access some data on firebase this is the data structure
I managed to access "favorite"
This is what I used to get
ref.observeEventType(.Value, withBlock: { snapshot in
let fav = snapshot.value.objectForKey("favPost")
print("\(fav) Printed")
})
but I'm trying to access "favPost" but I couldn't figure it out?!!
Firebase structure
{
"posts" : {
"-KGIxJybfQEJbcSy2bHH" : {
"author" : "Rioodi",
"postText" : "Test",
"votes" : 1
},
"-KGIxLUmPIa1Q1k0oRLs" : {
"author" : "Rioodi",
"postText" : "Raed",
"votes" : 0
},
"-KGJe-5ciAyu6Kom98E0" : {
"author" : "Neal",
"postText" : "Neal",
"votes" : 0
},
"-KGLFC0RqW_48lHMCsx8" : {
"author" : "Rioodi",
"postText" : "Test",
"votes" : 0
}
},
"users" : {
"afd0f27a-f62f-4cf1-9e81-032edc246687" : {
"email" : "test#test.com",
"favorite" : {
"-KGIxJybfQEJbcSy2bHH" : {
"favPost" : "Test"
}
},
"provider" : "password",
"username" : "Rioodi"
},
"fc56cc22-6275-48e0-8376-0b73b273b8e2" : {
"email" : "tests#test.com",
"provider" : "password",
"username" : "Neal"
}
}
}
Since you know the userId and you also know you are looking for their favorite post, directly access it like this.
let usersRef = self.myRootRef.childByAppendingPath("users")
let thisUserRef = usersRef.childByAppendingPath("this users id")
let thisUserFavoriteRef = thisUserRef.childByAppendingPath("favorite")
thisUserFavoriteRef.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
let fav = snapshot.value["favPost"] as! String
print(fav)
})
you could combine the path into a single line as well
let thisUserFavoriteRef = rootRef.childByAppendingPath("users/this users id/favorite")
You can try following code. (you can change according to requirements.)
ref.observeEventType( .Value, withBlock: { snapshot in
//lets consider you have reached up to "favarites"
let favarites = snapshot?.value as! [ String : [ String : AnyObject ] ]
//In "favarites" array you will get whole list
for favarite in favarites
{
let favPost = favarite.1[ "favPost" ] as? String //to access value part
print("\(favPost) Printed"
}
}