简体   繁体   English

Swift Firebase - 如何更新多个引用的值?

[英]Swift Firebase -How to Update a Value at multiple refs?

When a user first creates an account I check to see if the username they pick is available underneath all_usernames ref which is just a pool of all the users names.当用户第一次创建帐户时,我会检查他们选择的用户名是否在all_usernames ref 下可用,这只是所有用户名的池。 If the name isn't in there I add their userId and username to that ref (so no other users can pick it) and I also add it underneath the users ref (this is where their profile photo, username, etc would be for posts)如果名称不在那里,我将他们的 userId 和用户名添加到该 ref(因此其他用户无法选择它)并且我还将其添加到users ref 下方(这是他们的个人资料照片、用户名等用于发布帖子的位置)

-all_usernames
   |
   @--userId // kim_k's uid
       |
       |----username: "kim_k"

-users
   |
   @--userId // kim_k's uid
        |
        |---username: "kim_k"
        |---profileURL: "https//..."
        |---age: 27

My app has a followers and following ref.我的应用程序有一个关注者并关注参考。 I have a searchBar where users can search for people who are following them by name.我有一个搜索栏,用户可以在其中搜索按名称关注他们的人。

在此处输入图片说明

Everything works fine but I ran into hiccup.一切正常,但我遇到了打嗝。 Let's say the user they are following is "kim_k".假设他们关注的用户是“kim_k”。

let searchText = searchController.searchBar.text?.lowercased() // "kim_k"

let currentUserId = Auth.auth().currentUser!.uid // a different user

followingRef.child(currentUserId)
    .queryOrdered(byChild: "username")
    .queryStarting(atValue: searchText)
    .queryEnding(atValue: searchText+"\u{f8ff}")
    .observeSingleEvent(of: .value, with: { (snapshot) in ... }

Using the code above the currentUser who is performing the search will find "kim_k" underneath their following ref.使用执行搜索的 currentUser 上方的代码将在其following引用下方找到“kim_k”。 So will Jill and Jane (separate users) if they were to search for her name underneath their ref (the lat/lon locations are necessary for a separate reason):如果 Jill 和 Jane(不同的用户)要在他们的 ref 下搜索她的名字(因为单独的原因需要纬度/经度位置),他们也会这样做:

-following
    |
    @--userId // currentUserId
    |    |
    |    @----userId // kim_k's uid
    |           |
    |           |----username: "kim_k"
    |           |----lat: 0.111
    |           |----lon: 0.222
    |
    |
    @--userId // jill's uid
    |    |
    |    @----userId // kim_k uid
    |           |
    |           |----username: "kim_k"
    |           |----lat: 0.333
    |           |----lon: 0.444
    |
    @--userId // jane's uid
         |
         @----userId // kim_k's uid
                |
                |----username: "kim_k"
                |----lat: 0.555
                |----lon: 0.555

The hiccup is what happens if "kim_k" changes her name to "kim_kardashian"?如果“kim_k”将她的名字改为“kim_kardashian”会发生什么?

If she were to change it it would be updated under the users ref and the all_usernames ref.如果她要更改它,它将在users ref 和all_usernames ref 下更新。 When the currentUserId, Jill, and Jane search for her it would return her old username.当 currentUserId、Jill 和 Jane 搜索她时,它将返回她的旧用户名。

The "kim_kardashian" name change would have to happen at the users ref, all_usernames ref, following ref, and the followers refs.该“kim_kardashian”名称变更必须发生在users参考, all_usernames参考, following REF和followers裁判。

How can I fix this?我怎样才能解决这个问题?

To answer the question回答问题

Swift Firebase -How to Update a Value at multiple refs? Swift Firebase - 如何更新多个引用的值?

Updating multiple refs is easy.更新多个参考很容易。 Suppose you have a structure假设你有一个结构

items
   item_0
      item_name: "some item 0"
   item_1
      item_name: "some item 1"

and you want update both item names.并且您想要更新两个项目名称。 Here's the swift code.这是快速代码。

func updateMultipleValues() {
    let path0 = "items/item_0/item_name"
    let path1 = "items/item_1/item_name"

    let childUpdates = [ path0: "Hello",
                         path1: "World"
    ]

    self.ref.updateChildValues(childUpdates) //self.ref points to my firebase
}

and the result is结果是

items
   item_0
      item_name: "Hello"
   item_1
      item_name: "World"

EDIT编辑

There are acutally a couple of questions going on here so the above specifically shows how to update multiple refs at one time.这里实际上有几个问题,所以上面具体展示了如何一次更新多个引用。 However, that's actually unrelated to what the actual issue is.然而,这实际上与实际问题无关。

Don't store user names in multiple places (for this use case).不要在多个地方存储用户名(对于这个用例)。 There are times whan that's needed (denormalizing) but not here.有时需要(非规范化)但不是在这里。

The 10,000' view is as follows 10000'视图如下

If I want to know the names of the users who are following the current user, here's a start.如果我想知道关注当前用户的用户的姓名,请从这里开始。

/users
   uid_0 //assume this is the logged in user
      name: "My Name"
      followers
         uid_1 //uid_0 is being followed by uid_1
            some data
         uid_2
            some data
      following
         uid_3
  uid_1
      name: "Some other user name"
      followers
         whoever is following this user
      following
         uid_0 //uid_1 is following uid_0

then to get the names of who is following the the logged in user we'll use some pseudo code to keep it short.然后为了获得关注登录用户的人的姓名,我们将使用一些伪代码来保持简短。

let myFollowerNode = firebase.users.myuid.followers //read in the followers node
myFollowerNode.observeSingleEvent(.value, with { snapshot in
    //get the array of keys from the snapshot which would be
    let userIdArray = keys(snapshot)
    //  uid_1
    //  uid_2
    for uid in userIdArray { //iterate over the uid's, reading in their names
       let aUser = firebase.users.uid.name
       aUser.observeSingleEvent(.value..... 
           let name = snapshot.value as! string //that users name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM