I'm building a basic chat app with swift for iOS with firebase realtime database.
The Messages are observed with a limit for the least 10.
Now, I want to implement the functionality of loading earlier send messages. Currently I'm trying to achieve this by using this function:
let query = threadRef.child("messages").queryOrderedByKey().queryStarting(atValue: "2").queryLimited(toLast: 2)
Which returns this query:
(/vYhNJ3nNQlSEEXWaJAtPLhikIZi1/messages {
i = ".key";
l = 2;
sp = 2;
vf = r;
})
And this should give me the data:
query.observeSingleEvent(of: .value, with: { (snap) in
But it just limits the query and not set the start point to a specific position.
Here is the firebase database structure:
messages
-Kgzb3_b26CnkTDglNd8
date:
senderId:
senderName:
text:
-Kgzb4Qip6_jQdKRWFey
-Kgzb4ha0KZkLZeBIaxW
-Kgzb577KlNKOHxsQo9W
-Kgzb5cqIVMhRmU019Jf
Anyone have an idea on how to implement a feature like that?
Okay I finally found a way to do what I wanted.
First of all I misunderstood the way to access data from Firebase.
This is now how I get the query:
let indexValue = messages.first?.fireBaseKey
let query = messageRef.queryOrderedByKey().queryEnding(atValue:indexValue).queryLimited(toLast: 3)
1) get the FireBase key I previously saved to my custom chat messages
2) construct the query:
order it by key
set the ending to oldest message
limit the array to query to desired length
Then to actually get the query I used:
query.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children.dropLast().reversed() {
let fireSnap = (child as! FIRDataSnapshot)
//do stuff with data
}
})
1) get the query as a single event
2) iterate over children and I needed to dropLast() to make sure I don't have any duplicated messages and reverse it to get the correct order.
3) cast the current child as a FIRDataSnapshot to access the data
Since I couldn't find a simple example for this so I thought I leave my solution here incase other people running into the same problem.
Related
I have a query running to check .childAdded at a location in my database.
It works well when data is found, however, if there is not data at the location, it can't fire the query and therefore this does not allow me to use snapshot.exists because it doesn't even run the query.
This is my current code
let favouriteRef = self.databaseRef.child("users").child(userID!).child("Favourites")
// Code doesn't run past this line when no data at location
favouriteRef.queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
let favouriteID = "\(snapshot.value!)"
let usersRef = self.databaseRef.child("users")
usersRef.observeSingleEvent(of: .value, with: { (users) in
for user in users.children {
let favCleaner = UserClass(snapshot: user as! DataSnapshot)
if favouriteID == favCleaner.uid {
tempFav.append(favCleaner)
}
}
self.hasFavourites = true
self.tableView.reloadData()
self.usersArray = tempFav
self.collectionView.reloadData()
})
})
I would like to find a way to receive a callback if the query doesn't run (no data at location)
Thanks.
If there is absolutely no data at the location, then obviously this event trigger is not sufficient for you to get the data because this event only gets triggered when something gets added.
So you have two options
Add another trigger of type .observeSingleEvent(of: .value, with: { (snapshot) in
// Get the values and write the relevant actions
}
Or you can update this event to type .observe(.value, with: {snapshot in // write the relevant code
}
Both approaches have their advantage and disadvantage.
First approach will need you to write more code while minimising the number of triggers from your database to your UI. In second approach, there will be more triggers to the UI but it can be fairly easy to code.
From what I can see, you are first trying to establish whether data exists in the favorite node of your database and then comparing it to another snapshot. So if the number of delete or update events are relatively small, my suggestion is to go for approach two.
I am building an app that incorporates Firebase and need to be able to be able to add/and or set data at the top of the my database. Currently whenever I add data it places it randomly under the parent node. I've seen some Objective C answers to this but they haven't really made to much sense. Sorry for the lack of code, but I don't know where exactly where to start on this issue. Any help would be great!
When you say it is getting placed 'randomly' under the parent, I think this is the childByAutoId() method getting called. While the name of the nodes appears randomly (e.g. -K6tdghsbci7g8), it will actually ensure that data is added under a given parent node in order. This is very useful for adding new data to lists. For example:
let pointlessData:String = "some data"
ref.childByAutoId().setValue(pointlessData)
// creates a new ordered child node under the `ref` with "some data" as the value
There isn't really a way to order the nodes back to front in this fashion, although you could order negatively by timestamp by adding a negative timestamp to each node you add then order your results using ref.queryOrderedByChild("negativeTimestamp") such that the values you get are the newest first.
let path = FirebaseBaseUrl + "dialogs/" + self.receiverId + "/" + self.senderId
let chilRef = FIRDatabase.database().referenceFromURL(path)
let refChild = chilRef.childByAutoId()
let dic = NSMutableDictionary()
dic .setValue(text, forKey: "text")
dic .setValue(FIRServerValue.timestamp(), forKey: "timestamp")
dic .setValue(true, forKey: "your")
refChild.updateChildValues(dic as [NSObject : AnyObject]) { (error, ref) in
if(error != nil){
print("Error",error)
}else{
print("\n\n\n\n\nAdded successfully...")
}
}
I have this code in Swift 3 to get backendless user so I can get his/her properties:
let whereClause = "objectId = '\(userId)'"
let query = BackendlessDataQuery()
query.whereClause = whereClause
let data = Backendless.sharedInstance().persistenceService.of(BackendlessUser.ofClass())
data?.find(query, response: { (result) in
let user = result?.data.first as! BackendlessUser
myLabel.text = user.getProperty("firstName") as! String
})
The code is working fine but my question is how to observe the property changes ? is there a way if the value of property firstName changed I can update my label automatically ?
The use-case you describe is not really as simple as it may seem, but it's definitely possible.
You can capture any changes in Users table by creating an afterUpdate event handler. From there you could publish a message to some dedicated channel in messaging service.
At the same time, your application should be subscribed to this same channel. This way you could update your UI when the appropriate message is received.
My realtime database looks like this:
It contains only 1 child as you can see. I had 4 more children in RunningGames a few minutes before. I deleted them in the browser. When now calling this:
private lazy var runningGamesRef: FIRDatabaseReference = FIRDatabase.database().reference().child("RunningGames")
self.runningGamesRef.observeSingleEvent(of: .value, with: { (snapshot) -> Void in
for gameSnap in snapshot.children {
let id = (gameSnap as! FIRDataSnapshot).key
print(id)
}
})
It still prints those games I deleted in the browser. Calling runningGameRef!.removeValues() in my app does deletes it in the browser and on the iPhone (the print(id) is fixed). I have this error on multiple observeSingleEvent functions on different children, not only children of RunningGames. What would cause this annoying error?
Some children in RunningGames also have children, but they do auto remove themselves in the app. However, these values are also still visible when calling observeSingleEvent.
It's probably your local cache that's still holding the outdated info. This often happens when you're manipulating data from multiple sources.
I would try using observe instead of observeSingleEvent. I know it's a bit odd (and not really what you want if you only want to load the data once) but that should keep your info up to date.
Maybe by doing this you could fetch the info just once.
var handle: UInt = 0
handle = ref.observe(.value, with: { snapshot in
for gameSnap in snapshot.children {
let id = (gameSnap as! FIRDataSnapshot).key
print(id)
}
ref.removeObserver(withHandle: handle)
})
Source of the code (Frank van Puffelen)
I am designing a litte quiz app but I'm having trouble while retrieving the game data.
As you can see in the picture I have an JSON object that contains many single games. Each single game has a unique id. My first problem is that each of the games can be available in multiple languages. I know that I could download the hole snap and then looping throw each game but that would mean really long loading times while the app is growing.
In short form:
I need to retrieve the following data from the JSON above:
A random game wich is available in a specific language (need to have the key en for example)
All games that are available in "en" but not yet in "de"
If it is easier to restructure the data in the JSON, please tell me.
Thanks for helping me.
Answer to your first part :-
let enRef = FIRDatabase.database().reference().child("singleGames").child(singleGamesUID).child("en")
enRef.observeEventType(.Value, withBlock: {(snap) in
if let enQuizQuestion = snap.value as? [String:AnyObject]{
//Question exists : Retrieve Data
}else{
//Question in english doesn't exist
}
})
For your second part
Since you are trying to save iteration time might i suggest you also save your singleGames id in a separate languagesBased nodes, there is a command in firebase that allows you to search for some keyValues in your child node's , but even that i think would be executing a search algorithm which might be a little more time consuming :--
appServerName:{
singleGames :{
uid1:{......
......
...},
uid2:{......
......
...},
uid3:{......
......
...}
},
enQuestions:{
uid3 : true
}
deQuestions:{
uid1 : true,
uid2 : true
}
}
Now all you gotta do :-
let rootRef = FIRDatabase.database().reference().child("deQuestions").observeEventType(.Value, withBlock: {(qSnap) in
if let qDict = qSnap.value as? [String:AnyObject]{
for each in qDict as [String:AnyObject]{
let deUID = each.0
}
}else{
//No question in dutch language
}
})