0

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.

My basic database concept: enter image description here

Below is how I'm making my database: enter image description here

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"
    }
  }
}
André Luiz
  • 6,642
  • 9
  • 55
  • 105
  • See http://stackoverflow.com/questions/30299972/joining-data-between-paths-based-on-id-using-angularfire – Frank van Puffelen Nov 28 '16 at 17:44
  • @FrankvanPuffelenI saw that before asking, that's why I put a piece of json in the end of my question. It is not the same thing, although I'm trying, using NormalizedCollection – André Luiz Nov 28 '16 at 17:52
  • This is indeed a different join than Normalized Collection can handle. But the other option in that answer should work as I can see: you'll need to extend `$firebaseArray`. – Frank van Puffelen Nov 28 '16 at 18:02

1 Answers1

1

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.

André Luiz
  • 6,642
  • 9
  • 55
  • 105