本文介绍了同步数组(适用于喜欢/追随者)最佳实践[Firebase Swift]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Swift和Firebase创建一个基本的以下算法。我目前的实现如下:

$ p $ static func follow(user:FIRUser,userToFollow:FIRUser){
database。 child(users)。child(user.uid).observeSingleEventOfType(.Value,withBlock:{(snapshot)in
var dbFollowing:NSMutableArray!= snapshot.value![Follow] as!NSMutableArray!
dbFollowing?.addObject(userToFollow.uid)
self.database.child(users /+(user.uid)+/)。updateChildValues([Following:dbFollowing!])
//将用户uid添加到userToFollows追随者数组,以相似的方式
}){(error)in
print(follow - data could not be retrieve - EXCEPTION:+ error.localizedDescription)






这将从Firebase节点之后,添加 userToFollow 的uid,并将新数组发布到节点之后。这有几个问题:


  1. 它不是同步的,所以如果在两个设备上同时调用一个数组将会覆盖另一个并且追随者不会被保存。
  2. 如果没有追随者,它不能处理一个nil数组,程序将会崩溃(不是主要的关注点,我可以用optionals来解决)。 / li>

我想知道最好的做法是为用户追随者或发布喜欢创建一个同步的uid / token的数组。我发现了以下链接,但似乎没有直接解决我的问题,似乎也带来了其他问题。我认为这是明智的做法,就是向经验丰富的社区请教,而不是让Frankensteining一系列解决方案。

感谢您的帮助!

解决方案

感谢Frank,我想出了一个使用 runTransactionBlock 。这里是:

  static func follow(user:FIRUser,userToFollow:FIRUser){
self.database.child (users /+(user.uid)+/ following)。runTransactionBlock({(currentData:FIRMutableData!) - > FIRTransactionResult in
var value = currentData?.value as?Array< String>
if(value == nil){
value = [userToFollow.uid]
} else {
if!(value!.contains(userToFollow.uid)){$ b $ (value)$ b $(b)value!如果让错误=错误{
print(follow - update following transaction --excepTION:+ error.localizedDescription)
}
}
}

这会添加 userToFollow 到数组 Fo在用户。它可以处理nil值并相应地进行初始化,并且如果用户已经跟随了 userToFollow 的uid,将忽略该请求。

一些有用的链接:


I'm trying to create a basic following algorithm using Swift and Firebase. My current implementation is the following:

static func follow(user: FIRUser, userToFollow: FIRUser) {
    database.child("users").child(user.uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        var dbFollowing: NSMutableArray! = snapshot.value!["Following"] as! NSMutableArray!
        dbFollowing?.addObject(userToFollow.uid)
        self.database.child("users/"+(user.uid)+"/").updateChildValues(["Following":dbFollowing!])
        //add user uid to userToFollows followers array in similar way
    }) { (error) in
        print("follow - data could not be retrieved - EXCEPTION: " + error.localizedDescription)
    }
}

This retrieves the array of from Firebase node Following, adds the uid of userToFollow, and posts the new array to node Following. This has a few problems:

  1. It is not synchronized so if it is called at the same time on two devices one array will overwrite the other and followers will not be saved.
  2. If there are no followers it cannot deal with a nil array, the program will crash (not the main concern, I can probably address with optionals).

I was wondering what the best practice might be to created a synchronized array of uid/tokens for user followers or post likes. I found the following links, but none seem to directly address my problem and seem to carry other problems with it. I figured it would be wise to ask the community with experience instead of Frankensteining a bunch of solutions together.

Thanks for your help!

解决方案

Thanks to Frank, I figured out a solution using runTransactionBlock. Here it is:

static func follow(user: FIRUser, userToFollow: FIRUser) {
    self.database.child("users/"+(user.uid)+"/Following").runTransactionBlock({ (currentData: FIRMutableData!) -> FIRTransactionResult in
        var value = currentData?.value as? Array<String>
        if (value == nil) {
            value = [userToFollow.uid]
        } else {
            if !(value!.contains(userToFollow.uid)) {
                value!.append(userToFollow.uid)
            }
        }
        currentData.value = value!
        return FIRTransactionResult.successWithValue(currentData)
        }) { (error, committed, snapshot) in
            if let error = error {
            print("follow - update following transaction - EXCEPTION: " + error.localizedDescription)
        }
    }
}

This adds the uid of userToFollow to the array Following of user. It can handle nil values and will initialize accordingly, as well as will disregard the request if the user is already following the uid of userToFollow. Let me know if you have any questions!

Some useful links:

这篇关于同步数组(适用于喜欢/追随者)最佳实践[Firebase Swift]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:25