Upvote / Downvote系统内的Swift通过Firebase [英] Upvote/Downvote system within Swift via Firebase

查看:134
本文介绍了Upvote / Downvote系统内的Swift通过Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查了好几个小时的代码和笔记,我正在努力寻找任何文档来帮助我在firebase的快速应用中使用upvoting和downvoting对象。

我有一张照片画廊,我正在寻找增加一个instagram风格upvote图像。用户已经使用firebase身份验证进行了登录,所以我有他们的用户ID。



我只是努力想方法,什么规则需要在firebase中设置。 / p>

任何帮助都会很棒。

解决方案

在社交网络应用程序 Impether 中使用 Swift Firebase



由于upvoting和downvoting是类似的,所以我只描述upvoting。
$ b总的想法是将upvotes计数器直接存储在与计数器相关的图像数据对应的节点中,并使用事务性写入来更新计数器值例如,假设您将单个图像数据存储在路径 / images / <路径> <路径中, $ imageId / ,其中 $ imageId 是一个独特的i d用于标识特定的图像 - 可以通过例如 childByAutoId 。然后,在该节点上对应单张照片的对象看起来像:

  $ imageId:{
'url': 'http://static.example.com/images/$imageId.jpg',
'caption':'Some caption',
'author_username':'foobarbaz'
}

我们要做的是在这个节点上添加一个upvote计数器,所以它变成了:

  $ imageId:{
'url':'http://static.example.com/images/$imageId.jpg' ,
'caption':'Some caption',
'author_username':'foobarbaz',
'upvotes':12,
}
0
或其他一些常数取决于你想达到什么目的。



当更新一个特定的upvotes计数器时,想要使用交易,以避免不一致(当多个客户想同时更新一个计数器时会发生这种情况)。

幸运的是,处理 Firebase中的事务性写入和 Swift 非常简单:

  func upvote(imageId:String,
success successBlock:(Int) - >无效,
错误errorBlock:() - > Void){

let ref = Firebase(url:https://YOUR-FIREBASE-URL.firebaseio.com/images)
.childByAppendingPath(imageId)




ref.runTransactionBlock(
(currentData:FMutableData!)in

//更新之前计数器的值
var value = currentData.value as?Int

//使用
检查nil数据非常重要//事务性写入
if value == nil {
值= 0
}

//实际更新
currentData.value =值!+ 1
返回FTransactionResult.successWithValue(currentData)
}, andCompletionBlock:{
错误,已提交,捕获

//如果事务已提交,即$ snap中的
//变量的值为$ b之后的计数器$ b //更新完成
如果是commi特德{
let upvotes = snap.value as! Int
//调用成功回调函数,如果你想
successBlock(upvotes)
} else {
//调用错误回调函数,如果你想
errorBlock()



$ / code $ / pre

完全是我们在生产中使用的代码。我希望它能帮助你:)

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.

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.

I'm just struggling to figure the method and what rules need to be set in firebase.

Any help would be awesome.

解决方案

I will describe how I implemented such a feature in social networking app Impether using Swift and Firebase.

Since upvoting and downvoting is analogous, I will describe upvoting only.

The general idea is to store a upvotes counter directly in the node corresponding to an image data the counter is related to and update the counter value using transactional writes in order to avoid inconsistencies in the data.

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'
}

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,
}

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.

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).

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 :)

这篇关于Upvote / Downvote系统内的Swift通过Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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