【问题标题】:Updating Child Nodes in Firebase. Why is only one reference being updated and not both?在 Firebase 中更新子节点。为什么只有一个参考被更新,而不是两个?
【发布时间】:2019-07-31 22:55:23
【问题描述】:

我的 firebase 数据库中有用户。

我正在尝试添加好友请求功能。触摸好友请求按钮后,我希望数据库中两个用户的好友列表都进行相应修改

由于某种原因

Ref.child("users").child(currentUserUid!).child("Friends").updateChildValues([userClickedOnId! : false]) 

准确更新当前用户的好友列表。然而,第一句话,

Ref.child("users").child(userClickedOnId!).child("Friends").updateChildValues([currentUserUid : false]) 

不会更新在 firebase 数据库中点击的用户好友列表。

我添加了打印语句,一次尝试了一个语句。以 Ref 开头的语句。几乎相同,那么为什么只有我修改 currentUser 的朋友列表的第二条语句有效?

 @IBAction func funcconnectRequestButtonDidTapped(_ sender: Any) {
        let currentUserUid = Auth.auth().currentUser?.uid
        let userClickedOnId = uid
        let Ref = Database.database().reference()

        Ref.child("users").child(userClickedOnId!).child("Friends").updateChildValues([currentUserUid : false])
        Ref.child("users").child(currentUserUid!).child("Friends").updateChildValues([userClickedOnId! : false])
}

【问题讨论】:

  • 首先想到的是安全规则。您是否有允许用户仅写入自己的数据的安全规则?如果是这样,您还应该在客户端日志中找到一条错误消息,说明写入操作无效。
  • 我的读写安全设置都设置为 true
  • @LynnO'Brian 只需根据好友请求创建一个单独的节点待处理请求,并在接受该请求后将该节点添加到好友列表中。?

标签: swift firebase firebase-realtime-database


【解决方案1】:

您可以创建另一个节点friendRequests,在此节点内您可能有带有键 userUIDs 和数据senderUID1 : datelong, senderUID2 : datelong 的子节点,只需在您的应用程序 dbref.child('friendRequests/' + userUID).addChildEventListener 中侦听它,您将获得每个已经存在的好友请求 + 监听新请求。
让具有 parentID 的用户能够将 senderUUID 的值写入 true 或“已接受”,w/e 适合您。
然后创建一个云函数(触发器)以在friendRequests/{userUID}/{senderUID} 上监听 onUpdate,如果值为 true,则将每个用户的 id 添加到两个朋友列表中。如果为假,请删除此参考。
由于根据您的数据结构编写所有这些代码将花费大量时间,因此我将为您提供已经存在的代码,但您必须重写它: 这些是规则:

"friendList": {
  "$uid":{
    ".read" : "$uid === auth.uid",
    "$friendUID": {
      ".write": "$uid === auth.uid && data.exists() && newData.val() == false"
    }
  }
},

"friendRequest": {
  "$uid": {
    ".read" : "$uid === auth.uid",
    "$senderUID": {
      ".write" : "$senderUID === auth.uid && $senderUID != $uid && !data.exists() && root.child('userInfo').child($uid).exists() && !root.child('blockList').child($uid).child($senderUID).exists() && !root.child('blockList').child($senderUID).child($uid).exists() && !root.child('friendList').child($uid).child($senderUID).exists() && !root.child('friendList').child($senderUID).child($uid).exists() && !root.child('friendRequest').child($senderUID).child($uid).exists()",
      ".validate": "newData.hasChildren(['message', 'date'])",
      "message": {
        ".validate": "newData.isString() && newData.val().length < 51 && newData.val().length > 0"
      },
      "date": {
        ".validate": "newData.val() == now" // ServerValue.TIMESTAMP
      },
      "accepted": {
        ".write" : "$uid === auth.uid && $uid != $senderUID && (newData.val() == true || newData.val() == false) && !data.exists() && root.child('friendRequest').child($uid).child($senderUID).exists()"
      },
      "$other": {
        ".validate": false
      }

    }
  }
},

"blockList":{
  "$uid":{
    ".read": "$uid === auth.uid",
    "$blockUID": {
      ".write" : "$uid === auth.uid && $uid != $blockUID && ( ((!data.exists() && newData.val() == true) || (data.exists() && !newData.exists())) ) && root.child('userInfo').child($blockUID).exists()"
    }
  }
},

如您所见,这些规则不是很好,但您可以重写它们以适应您的情况,并在我所见的范围内删除一些无用的代码。
现在云功能:

exports.dbWriteOnFriendRequestChoice = functions.database.ref('/friendRequest/{receiverid}/{senderid}/accepted').onWrite((change, context) => {
    if (!change.after.exists()) {
        return null;
    }
    const writtenContent = change.after.val(); // true || false
    const receiverid = context.params.receiverid;
    const senderid = context.params.senderid;

    if(writtenContent) {
        const promiseAccepted1 = admin.database().ref('/friendList/' + senderid + '/' + receiverid).set(true);
    const promiseAccepted2 = admin.database().ref('/friendList/' + receiverid + '/' + senderid).set(true);
    const promiseRefused = admin.database().ref('/friendRequest/' + receiverid + '/' + senderid).remove();
        return Promise.all([promiseAccepted1, promiseAccepted2, promiseRefused]);
    } else {
        const promiseRefused = admin.database().ref('/friendRequest/' + receiverid + '/' + senderid).remove();
        return Promise.all([promiseRefused]);
    }
});

exports.dbWriteOnBlock = functions.database.ref('/blockList/{useruid}/{blockuid}').onWrite((change, context) => {
    if (!change.after.exists()) {
        return null;
    }
    //const writtenContent = change.after.val(); // true || false
    const useruid = context.params.useruid;
    const blockuid = context.params.blockuid;

    const promiseFriendList1 = admin.database().ref('/friendList/' + useruid + '/' + blockuid).remove();
    const promiseFriendList2 = admin.database().ref('/friendList/' + blockuid + '/' + useruid).remove();
    return Promise.all([promiseFriendList1, promiseFriendList2]);
    /*const promiseUnblock = admin.database().ref('/blockList/' + useruid + '/' + blockuid).remove();

    if(writtenContent) {
        return Promise.all([promiseFriendList1, promiseFriendList2]);
    } else {
        return Promise.all([promiseUnblock]);
    }*/
});

exports.dbWriteOnUnfriend = functions.database.ref('/friendList/{useruid}/{frienduid}').onWrite((change, context) => {
    if (!change.after.exists()) {
        return null;
    }
    const writtenContent = change.after.val(); // true || false
    const useruid = context.params.useruid;
    const frienduid = context.params.frienduid;

    if(!writtenContent) {
        const promiseReove1 = admin.database().ref('/friendList/' + useruid + '/' + frienduid).remove();
        const promiseReove2 = admin.database().ref('/friendList/' + frienduid + '/' + useruid).remove();
        return Promise.all([promiseReove1, promiseReove2]);
    } else {
        return null;
    }
});

您可以删除阻止并执行您的操作。这适用于 100%,因为我已经在这个应用程序中对其进行了测试:https://www.youtube.com/watch?v=vGCNsD_eeSA

【讨论】:

  • 感谢您提供有用的反馈!我想我能够解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 2018-08-08
  • 2022-12-09
  • 1970-01-01
相关资源
最近更新 更多