通过 Firebase 在 Swift 中点赞/反对系统 [英] Upvote/Downvote system within Swift via Firebase
问题描述
我已经查看了数小时的代码和注释,但我一直在努力寻找任何文档来帮助我在带有 firebase 的 swift 应用程序中对对象进行投票和投票.
I've looked over hours of code and notes and I'm struggling to find any documentation that would help me with upvoting and downvoting an object in a swift app with firebase.
我有一个照片库,我想为图像添加 Instagram 风格的赞.用户已经使用 firebase auth 登录,所以我有他们的用户 ID.
I have a gallery of photos and I'm looking to add an instagram style upvote to images. The user has already logged with firebase auth so I have their user ID.
我只是在努力弄清楚方法以及需要在 firebase 中设置哪些规则.
I'm just struggling to figure the method and what rules need to be set in firebase.
任何帮助都会很棒.
推荐答案
我将描述我如何在社交网络应用程序中实现这样的功能 Impether 使用 Swift
和 Firebase
.
I will describe how I implemented such a feature in social networking app Impether using Swift
and Firebase
.
因为upvoting 和downvoting 是类似的,所以我只会描述upvoting.
Since upvoting and downvoting is analogous, I will describe upvoting only.
总体思路是直接在与计数器相关的图像数据对应的节点中存储一个 upvotes 计数器,并使用事务写入更新计数器值,以避免数据不一致.
例如,假设您在路径 /images/$imageId/
中存储单个图像数据,其中 $imageId
是用于标识特定图像 - 例如可以通过函数 childByAutoId生成a> 包含在适用于 iOS 的 Firebase 中.然后在该节点对应于单张照片的对象看起来像:
For example, let's assume that you store a single image data at path /images/$imageId/
, where $imageId
is an unique id used to identify a particular image - it can be generated for example by a function childByAutoId included in Firebase for iOS. Then an object corresponding to a single photo at that node looks like:
$imageId: {
'url': 'http://static.example.com/images/$imageId.jpg',
'caption': 'Some caption',
'author_username': 'foobarbaz'
}
我们想要做的是给这个节点添加一个upvote counter,这样就变成了:
What we want to do is to add an upvote counter to this node, so it becomes:
$imageId: {
'url': 'http://static.example.com/images/$imageId.jpg',
'caption': 'Some caption',
'author_username': 'foobarbaz',
'upvotes': 12,
}
当您创建一个新图像时(可能是当用户上传它时),那么您可能需要使用 0
或其他一些常量来初始化 upvote 计数器值,具体取决于您想要实现的目标.
When you are creating a new image (probably when an user uploads it), then you may want to initialize the upvote counter value with 0
or some other constant depending on what are you want to achieve.
在更新特定的 upvotes 计数器时,您希望使用事务以避免其值不一致(当多个客户端想要同时更新计数器时可能会发生这种情况).
When it comes to updating a particular upvotes counter, you want to use transactions in order to avoid inconsistencies in its value (this can occur when multiple clients want to update a counter at the same time).
幸运的是,在 Firebase
和 Swift
中处理事务写入非常简单:
Fortunately, handling transactional writes in Firebase
and Swift
is super easy:
func upvote(imageId: String,
success successBlock: (Int) -> Void,
error errorBlock: () -> Void) {
let ref = Firebase(url: "https://YOUR-FIREBASE-URL.firebaseio.com/images")
.childByAppendingPath(imageId)
.childByAppendingPath("upvotes")
ref.runTransactionBlock({
(currentData: FMutableData!) in
//value of the counter before an update
var value = currentData.value as? Int
//checking for nil data is very important when using
//transactional writes
if value == nil {
value = 0
}
//actual update
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
}, andCompletionBlock: {
error, commited, snap in
//if the transaction was commited, i.e. the data
//under snap variable has the value of the counter after
//updates are done
if commited {
let upvotes = snap.value as! Int
//call success callback function if you want
successBlock(upvotes)
} else {
//call error callback function if you want
errorBlock()
}
})
}
上面截取的代码实际上几乎就是我们在生产中使用的代码.希望对你有帮助:)
The above snipped is actually almost exactly the code we use in production. I hope it helps you :)
这篇关于通过 Firebase 在 Swift 中点赞/反对系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!