如何获取新通知的收据

时间:2016-10-14 20:05:16

标签: firebase firebase-realtime-database swift3

Ayyyy,伙计们!我想知道Firebase中是否有办法将新子项的数量添加到对象中。反正有没有这样做?请告诉我是否有!

如果将新对象添加到一组对象上,我的意思是更合乎逻辑的方式(愚蠢的我!)。

User object

在上面附带的图片中,我试图将新元素的数量添加到'通知'上面的节点。

我的尝试:

FIRDatabase.database().reference().child("users").child(self.uid!).child("notifications").observe(.childAdded, with: { (snapshot) in

            let amountOfNotifications = snapshot.childrenCount

        })

JSON:

{
  "email" : "asdfasdf2ladjflj@aldkfj.org",
  "followers" : {
    "alsdkf" : "lkajsdlfj@ljasldf.o",
    "asdfasdf" : "ljfklasdjl;ajJ@L:KJl;kaf.daisodf",
    "asdfasdfk" : ";lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc",
    "dgsddfaf" : "lk;ajdskfljaslkj@Jlk;ajdlkfa.io",
    "follower1" : "roxyofficial@gmail.com",
    "follower10z" : "somethings@cats.io",
    "follower2" : "test@test.com",
    "follower3" : "test@test.zed",
    "follower4" : "l;kajsfkljasdflk;j@KL:Jlkajdsfl;kasdf.oio",
    "follower7" : "zed@zed.io",
    "followerte" : "testfollower@test.io",
    "folower8" : "zed>!@zedl.ocm",
    "test" : "test@io.c"
  },
  "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(somethings@cats.io) followed you!",
    "let notification 1680115082 = new follower" : "Optional(l;kajsfkljasdflk;j@KL:Jlkajdsfl;kasdf.oio) followed you!",
    "let notification 1730793657 = new follower" : "Optional(testfollower@test.io) followed you!",
    "let notification 1820028610 = new follower" : "Optional(test@test.zed) followed you!",
    "let notification 1975523553 = new follower" : "Optional(zed@zed.io) followed you!",
    "let notification 2129596138 = new follower" : "Optional(test@test.zed) followed you!",
    "let notification 2181363532 = new follower" : "Optional(;lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc) followed you!",
    "let notification 2367950713 = new follower" : "Optional(test@io.c) followed you!",
    "let notification 2409722982 = new follower" : "Optional(;lkjaksldjfkl;ajsdlkf@LJ;lkjlka;jdf.omc) followed you!",
    "let notification 2431788206 = new follower" : "Optional(zed>!@zedl.ocm) 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(test@test.com) followed you!",
    "let notification 2835016479 = new follower" : "Optional(testfollower@test.io) followed you!",
    "let notification 2914050639 = new follower" : "Optional(roxyofficial@gmail.com) followed you!",
    "let notification 2954110099 = new follower" : "Optional(test@test.com) followed you!",
    "let notification 2963247743 = new follower" : "Optional(zed@zed.io) followed you!",
    "let notification 3251218202 = new follower" : "Optional(zed>!@zedl.ocm) followed you!",
    "let notification 340385741 = new follower" : "Optional(testfollower@test.io) followed you!",
    "let notification 3424214838 = new follower" : "Optional(somethings@cats.io) followed you!",
    "let notification 3491288550 = new follower" : "Optional(test@test.com) followed you!",
    "let notification 3734762074 = new follower" : "Optional(ljfklasdjl;ajJ@L:KJl;kaf.daisodf) followed you!",
    "let notification 3788313156 = new follower" : "Optional(roxyofficial@gmail.com) 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(test@io.c) 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(test@test.zed) followed you!",
    "let notification 509688728 = new follower" : "Optional(roxyofficial@gmail.com) followed you!",
    "let notification 599767405 = new follower" : "Optional(somethings@cats.io) followed you!",
    "let notification 745520426 = new follower" : "Optional(zed@zed.io) followed you!",
    "let notification 882753091 = new follower" : "Optional(test@io.c) followed you!",
    "let notification 937740183 = new follower" : "Optional(zed>!@zedl.ocm) followed you!",
    "let notification = new follower" : "Optional(test@io.c) followed you!",
    "not" : "test",
    "notification1" : "Welcome to platform!",
    "notification2" : "someIdiotFollowedYou!",
    "notification3" : "test",
    "testNot" : "alksdjf;klajsdf",
    "testNoti" : "test"
  },
  "peopleFollowing" : {
    "following1" : "dnalwodza@gmail.com",
    "following2" : "roxyofficial@gmail.com"
  },
  "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"
}

1 个答案:

答案 0 :(得分:2)

我假设用户必须点击一个按钮来检查其通知,并且每次点击您都会发送搜索它的查询。

在JSON结构上保留一个单独的节点,

 {
    "email" : "asdfasdf2ladjflj@aldkfj.org",
    "followers" : {...},
    "name" : "ASDFASDF",
    "notifications" : {...},
    "peopleFollowing" : {...},
    "noOfFollwersWhenLastChecked" : 100,    //
    "profileImageUrl" : 
    "userid" : "RIjJxqqyVdVnwqpB507SRkl2Zhf1"
  }

每当您的用户访问notifications节点时,请计算notification节点中的节点数并减去noOfFollwersWhenLastChecked值,并将其值替换为count那里的节点数在notification