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. 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)
-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".
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. 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):
-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"?
If she were to change it it would be updated under the users
ref and the all_usernames
ref. When the currentUserId, Jill, and Jane search for her it would return her old username.
The "kim_kardashian" name change would have to happen at the users
ref, all_usernames
ref, following
ref, and the followers
refs.
How can I fix this?