新留言台 [英] New message counter

查看:46
本文介绍了新留言台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的社交应用程序(使用firebase和jsqmessagesviewcontroller),并试图使新消息"成为问题. firebase中是否有任何内置功能可检测是否添加了新子级?我试图以这种方式制作:

I'm working on a simple social app (using firebase and jsqmessagesviewcontroller) and trying to make "new message" counter. Is there any build-in features in firebase to detect if new child was added? I've tried to make in a such way:

ref.observe(.childAdded, with: { (snapshot) -> Void in
  self.newMsg += 1
})

但是此行发现所有消息,不仅是新消息.是否有任何内置功能可以帮助我解决此问题?预先感谢.

But this line founds all messages, not only new. Is there any build-in feature which help me to handle with this problem? Thanks in advance.

推荐答案

没有解决此问题的方法.但是我对如何解决这个问题有一些想法.

There is no method for this problem. But I have some ideas on how to approach this.

可能性A:

我建议您将可用子级的值存储在设备上. (您可以使用 UserDefaults .)然后,如果您检查新的孩子,则会得到新的所有孩子的数量.您只需要在代码newValueOfChildren - valueOfChildren中进行此计算即可.如果结果相等,您就知道没有新孩子.如果结果不相等,则现在您添加了多少个新子代.计算之后,您用newValueOfChildren

I would recommend that you store the value of available children on your device. (You could use UserDefaults.) Then if you check for new children you get the new number of all children. You just need do make this calculation in code: newValueOfChildren - valueOfChildren. If the result is equal you know there is no new child. If the result is not equal you now how much new children were added. After this calculation you overwrite valueOfChildren from UserDefaults with newValueOfChildren

让我们初始化变量:

let valueOfChildren = UserDefaults.standard
var newValueOfChildren: Int!

让我们说您当前在Firebase实时数据库中存储了6个孩子.

Let us say you have currently 6 children stored in the Firebase Realtime Database.

//How to set user defaults:
valueOfChildren.setInteger(6, forKey: "valueOfChildren")

现在,我添加了三个孩子.因此,Firebase数据库中现在存储了9个子级.您用来计算现有子代的代码必须如下所示:

Now I added three children. So there are now 9 children stored in the Firebase Database. Your code to count the existing children must look like this:

var newValueOfChildren: Int!
ref.observe(.value, with: { (snapshot: FIRDataSnapshot!) in
    newValueOfChildren = snapshot.childrenCount
}

现在让我们从UserDefaults获取当前Int并将它们分配给新变量(calcValueA).之后,我们将计算:

Let us now get the current Int from UserDefaults and assign them to a new variable (calcValueA). After that we will calculate:

//How to get user defaults:
var calcValueA = defaults.integerForKey("valueOfChildren")

print(newValueOfChildren - calcValueA)

print()语句将返回以下内容:

The print() statement will return this:

3

将变量命名略有不同可能很有用. :-)

Maybe it's useful to name the variables a bit different. :-)

可能性B:

您可以Firebase Cloud Functions并在服务器上使用一个功能,该功能类似于上述方法进行更新和工作,或者对数据库中的计数器值加1来响应新的子代.您可以简单地将其存储在数据库中并从设备读取.

You could Firebase Cloud Functions and use a function on the server that updates and works similar to the described method above or respond to new children with adding 1 to a counter value in your database. You could simply store this in your database and read from a device.

查看Firebase团队构建的以下示例:

Check out these examples the Firebase team build:

https://github.com/firebase/functions-samples/

这篇关于新留言台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆