Firebase - how to get the key value in observeEventType = Value
In if condition, you need to get allKeys to get "-KKMkpA22PeoHtBAPyKm" ...
if snapshot.exists() {
for a in (snapshot.value?.allKeys)!{
print(a)
}
} else {
print("we don't have that, add it to the DB now")
}
Getting key value but not the root value of Firebase
To get all room names ...You have to get allKeys like
snapshot.value.allKeys()
This will give you ["user1_user2","user1_user3"]
i.e your roomnames.
Firebase database query by key value
First go till child "livefeeds" and then "queryOrderedByChild" category and use equal to
child like this way
[[[[self.ref child:@"livefeeds"] queryOrderedByChild:@"category"]
queryEqualToValue:groupId]
observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
if (snapshot.value != [NSNull null]){
for (NSDictionary *snap in [snapshot.value allValues]) {
NSLog(@"---> %@",snap);
}
}
}];
How to query by key string instead of value in firebase?
This takes a bit of reframing of the problem. Instead of saying "I want the items with this property", think of it as "I want the items with whatever value for this property".
Query firebaseSearchQuery = mUserDatabase.orderByChild("exp/banana").startAt(" ")
Here the space (" "
) is just the first printable character, so we're matching everything.
While this query will work, it requires that you define an index on exp/banana
for each user. It's very likely that you'll get better results if you create an inverted index in your JSON to allow this specific lookup. For a longer explanation and an example, see my answer here: Firebase query if child of child contains a value
How get data in Firebase?
Move this to viewDidLoad
FIRDatabaseReference *ref = [[FIRDatabase database] reference];
Check whether you configured everything according to the documentation.
Check the key names
If everything is fine then try changing this
Try with observeSingleEventOfType instead of observeEventType[[[self.ref child:@"buysell"] child:@"users"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSDictionary *dict = snapshot.value;
NSLog(@"%@",dict);
} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];If above 3 not works check your error statement
Firebase - accessing child node when you don't know the value of the parent key - generated by childByAutoId()
I think you're looking for something like:
ref.child("friendRequest")
.child(FIRAuth.auth()?.currentUser.uid)
.queryOrdered(byChild: "fromId")
.queryEqual(toValue: "theUidThatYou'reLookingFor")
.observeSingleEvent(of: .childAdded, with: { (snapshot) in
print("\(snapshot.key)")
})
Firebase with Swift ambiguous use of observeEventType
Just some coding errors
Remove: (snapshot)-> Void
Change: child in snapshot as snapshot is not a sequence, whereas snapshot.children is
I assume you want to store the friends name as a string and name is a key in your structure. So change self.friendsList.append(child.value) to
let name = child.value["name"] as? String
friendsList.append(name!)
Here's the corrected code:
var friendsList = [String]()
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
if snapshot.value is NSNull {
} else {
for child in snapshot.children {
let name = child.value["name"] as? String
friendsList.append(name!)
}
print("\(friendsList)")
}
})
Firebase snapshot.key not returning actual key?
When you run a query at a location, the result will be a list of the matching children. Even if there is only a single matching item, the result will be a list of one child.
You're printing the key of all resulting children. Since there is no single result, the SDK prints the key of the location/collection that you queried: users
.
What you're likely looking for is to loop over the matching children and print their keys:
let query = usersRef.queryOrderedByChild("email").queryEqualToValue(email)
query.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
print(child.key)
}
})
How to read child or filter firebase data by its key value in swift ios
Since you want to execute the same code for each item, you'll want to use .ChildAdded
:
ref.queryOrderedByChild("price").observeEventType(.ChildAdded, withBlock: { snapshot in
if let price = snapshot.value["price"] as? Int {
println("\(snapshot.key) price at \(price) Dollars ")
println(snapshot.key)
}
})
See the page on retrieving data in the Firebase guide for iOS developers for more information and examples.
Update
I ended up using your code in a local xcode and see there are two problems. So all three combined:
you are listening for the
.Value
event, but your block is dealing with a single item at a time. Solution:ref.queryOrderedByChild("price")
.observeEventType(.ChildAdded, withBlock: { snapshot inyou are listening for the
.Value
event at the top-level, but you are adding the items underusers
. Solution:ref.childByAppendingPath("users")
.queryOrderedByChild("price")
.observeEventType(.ChildAdded, withBlock: { snapshot inyou are testing whether the price is an
Int
, but are adding them as strings. Solution:var item1 = ["name": "Alan Turning", "item" : "Red Chair", "price": 100]
var item2 = ["name": "Grace Hopper", "item": "Sofa Bed" , "price": 120]
var item3 = ["name": "James Cook" , "item": "White Desk", "price": 250]
var item4 = ["name": "James Cook" , "item": "Mattress Cal King", "price": 100]
With those changes, the code prints out these results for me:
item1 price at 100 Dollars
item1
item4 price at 100 Dollars
item4
item2 price at 120 Dollars
item2
item3 price at 250 Dollars
item3
Related Topics
Get Each Line of Text in a Uilabel
Create Custom International Keyboard for Iphone
Constraints for Centering One Button When the Other Is Hidden
Audiokit iOS Aksamplermetronome
How to Crop a Uiimage in Swift
Aws Cognito iOS Developer Authenticated Identities
How to Connect SQLite Database in iOS
Check If Username Already Exist'S:Swift, Firebase
Firebase Queryorderedbychild() Method Not Giving Sorted Data
Swipe Left or Right to Load the View Controller with the Collection View Cell Highlight
How to Make Alamofire Download Progress Run in Background iOS
Are Storyboards Going to Work on iOS 4
Nsurlsession Delegates Not Called
Changing iPad/iPhone Simulator Resolution for Xcode 4.3.2
iOS Are Methods Called by Delegates and Observers Executed on the Main Thread
Uibutton - Alloc Initwithframe: VS. Buttonwithtype:
Swift 3 - Alamofilre 4.0 Multipart Image Upload with Progress