Reputation: 7292
I'm working in a project where I have these entities: region, messages and a link between region and messages. In this link I also can configure some extra properties such as if the message can be show more than once for a region and if there is a pre-requisite message to be show before.
Below is how I'm making my database:
I would like to be able to show a list with the message name, the show flag and the name of the pre-requisite message, if filled.
I'm using the $firebaseArray object to do my regular lists and I have seen examples on how to do the queries when you have a structure like this:
"region_messages":{
"xxxxxxx":true,
"yyyyyyy":true
}
How could I do in my case, my structure is more complex. I'm trying to query using angularfire and I will need to do the same in Swift 3.
Thanks for any help!
Here is my json tree in case you want to run some tests:
{
"messages" : {
"-KXSIeKHTM4lMRbeey2k" : {
"active" : true,
"name" : "link",
"type" : 3,
"url" : "http://www.google.com"
},
"-KXSIi_qw369nfU28lJJ" : {
"active" : true,
"name" : "video",
"type" : 4,
"url" : "http://www.youtube.com"
}
},
"region_messages" : {
"-KXfZYP8e--ZUgaVM9iL" : {
"-KXSIeKHTM4lMRbeey2k" : {
"pre_requisite_message_id" : "",
"show_only_once" : false
},
"-KXSIi_qw369nfU28lJJ" : {
"pre_requisite_message_id" : "-KXSIeKHTM4lMRbeey2k",
"show_only_once" : true
}
}
},
"regions" : {
"-KXfZYP8e--ZUgaVM9iL" : {
"major" : 1,
"name" : "Region 1"
}
}
}
Upvotes: 0
Views: 555
Reputation: 7292
I followed the instruction in this video and I managed to solve the problem. It sounds weird to me to make subsequent calls but I guess there is not much troubles with it. The function:
$scope.getMessagesByRegion = function(regionId){
var rootRef = firebase.database().ref();
var regionMessagesRef = rootRef.child("region_messages/"+ regionId);
$scope.messages_by_region = []; // Here I reset the scope variable
regionMessagesRef.on('child_added', function(rmSnap) {
var messageRef = rootRef.child("messages/"+rmSnap.key);
messageRef.once('value').then(function(msgSnap){
var msg = {
key : msgSnap.key,
name : msgSnap.val().name,
type : $scope.getTypeName(msgSnap.val().type),
show_only_once : rmSnap.val().show_only_once,
pre_requisite_message : rmSnap.val().pre_requisite_message
}
$scope.$apply(function(){
$scope.messages_by_region.push(msg);
});
})
});
}
For swift (iOS) I had to do the same. The performance seems good so far.
Upvotes: 1