Reputation: 440
Ayyyy, mates! I was wondering if there was a way in Firebase to get the amount of new children added to an object. Is there anyway to do this? Please tell me if there is!
What I mean in a more logical way (silly me!) if a new object is added onto a group of objects.
In the image attached above, I'm trying to get the amount of new elements added to the 'notifications' node above.
My Attempt:
FIRDatabase.database().reference().child("users").child(self.uid!).child("notifications").observe(.childAdded, with: { (snapshot) in
let amountOfNotifications = snapshot.childrenCount
})
JSON:
{
"email" : "[email protected]",
"followers" : {
"alsdkf" : "[email protected]",
"asdfasdf" : "ljfklasdjl;ajJ@L:KJl;kaf.daisodf",
"asdfasdfk" : ";lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc",
"dgsddfaf" : "lk;ajdskfljaslkj@Jlk;ajdlkfa.io",
"follower1" : "[email protected]",
"follower10z" : "[email protected]",
"follower2" : "[email protected]",
"follower3" : "[email protected]",
"follower4" : "l;kajsfkljasdflk;j@KL:Jlkajdsfl;kasdf.oio",
"follower7" : "[email protected]",
"followerte" : "[email protected]",
"folower8" : "zed>[email protected]",
"test" : "[email protected]"
},
"name" : "ASDFASDF",
"notifications" : {
"let notification 1343936250 = new follower" : "Optional(lk;ajdskfljaslkj@Jlk;ajdlkfa.io) followed you!",
"let notification 1588601033 = new follower" : "Optional(;lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc) followed you!",
"let notification 1644747852 = new follower" : "Optional([email protected]) followed you!",
"let notification 1680115082 = new follower" : "Optional(l;kajsfkljasdflk;j@KL:Jlkajdsfl;kasdf.oio) followed you!",
"let notification 1730793657 = new follower" : "Optional([email protected]) followed you!",
"let notification 1820028610 = new follower" : "Optional([email protected]) followed you!",
"let notification 1975523553 = new follower" : "Optional([email protected]) followed you!",
"let notification 2129596138 = new follower" : "Optional([email protected]) followed you!",
"let notification 2181363532 = new follower" : "Optional(;lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc) followed you!",
"let notification 2367950713 = new follower" : "Optional([email protected]) followed you!",
"let notification 2409722982 = new follower" : "Optional(;lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc) followed you!",
"let notification 2431788206 = new follower" : "Optional(zed>[email protected]) followed you!",
"let notification 2469280631 = new follower" : "Optional(ljfklasdjl;ajJ@L:KJl;kaf.daisodf) followed you!",
"let notification 2750686409 = new follower" : "Optional(l;kajsfkljasdflk;j@KL:Jlkajdsfl;kasdf.oio) followed you!",
"let notification 2800426934 = new follower" : "Optional([email protected]) followed you!",
"let notification 2835016479 = new follower" : "Optional([email protected]) followed you!",
"let notification 2914050639 = new follower" : "Optional([email protected]) followed you!",
"let notification 2954110099 = new follower" : "Optional([email protected]) followed you!",
"let notification 2963247743 = new follower" : "Optional([email protected]) followed you!",
"let notification 3251218202 = new follower" : "Optional(zed>[email protected]) followed you!",
"let notification 340385741 = new follower" : "Optional([email protected]) followed you!",
"let notification 3424214838 = new follower" : "Optional([email protected]) followed you!",
"let notification 3491288550 = new follower" : "Optional([email protected]) followed you!",
"let notification 3734762074 = new follower" : "Optional(ljfklasdjl;ajJ@L:KJl;kaf.daisodf) followed you!",
"let notification 3788313156 = new follower" : "Optional([email protected]) followed you!",
"let notification 3862281533 = new follower" : "Optional(l;kajsfkljasdflk;j@KL:Jlkajdsfl;kasdf.oio) followed you!",
"let notification 3863677856 = new follower" : "Optional(lk;ajdskfljaslkj@Jlk;ajdlkfa.io) followed you!",
"let notification 3867733078 = new follower" : "Optional([email protected]) followed you!",
"let notification 3942761365 = new follower" : "Optional(lk;ajdskfljaslkj@Jlk;ajdlkfa.io) followed you!",
"let notification 4066943532 = new follower" : "Optional(ljfklasdjl;ajJ@L:KJl;kaf.daisodf) followed you!",
"let notification 4083932515 = new follower" : "Optional([email protected]) followed you!",
"let notification 509688728 = new follower" : "Optional([email protected]) followed you!",
"let notification 599767405 = new follower" : "Optional([email protected]) followed you!",
"let notification 745520426 = new follower" : "Optional([email protected]) followed you!",
"let notification 882753091 = new follower" : "Optional([email protected]) followed you!",
"let notification 937740183 = new follower" : "Optional(zed>[email protected]) followed you!",
"let notification = new follower" : "Optional([email protected]) followed you!",
"not" : "test",
"notification1" : "Welcome to platform!",
"notification2" : "someIdiotFollowedYou!",
"notification3" : "test",
"testNot" : "alksdjf;klajsdf",
"testNoti" : "test"
},
"peopleFollowing" : {
"following1" : "[email protected]",
"following2" : "[email protected]"
},
"profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/platform-ad9e3.appspot.com/o/profile_images%2FprofileImageRIjJxqqyVdVnwqpB507SRkl2Zhf1.jpeg?alt=media&token=ea86df9e-4949-4409-b92d-3ed8db64f5de",
"rank" : "Laksjdfklja;add",
"userid" : "RIjJxqqyVdVnwqpB507SRkl2Zhf1"
}
Upvotes: 0
Views: 87
Reputation: 9945
I am presuming that its a button which a user has to click to check its notification and every time he clicks you send a query searching for it.
Keep a separate node on your JSON structure ,
{
"email" : "[email protected]",
"followers" : {...},
"name" : "ASDFASDF",
"notifications" : {...},
"peopleFollowing" : {...},
"noOfFollwersWhenLastChecked" : 100, //
"profileImageUrl" :
"userid" : "RIjJxqqyVdVnwqpB507SRkl2Zhf1"
}
Every time your user access notifications
node , count the no of nodes there in the notification
node and substract with noOfFollwersWhenLastChecked
value, and replace its value with count the no of nodes there in the notification
.
Upvotes: 2