Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 13 characters in body
Source Link
Jay
  • 35.8k
  • 19
  • 61
  • 89

Array's can be challenging in Firebase as the individual elements cannot be accessed directly or modified. You either read the whole array or write the whole array.

I would suggest changing the Structure to better match the data you want and avoid array's entirely:

"UserProfile" : { "D8QmnOSH6vRYiMujKNXngzhdn992" : { "age" : "12", "email" : "[email protected]", "gender" : "f", "password" : "123454321", "typeOfBackpacker" : "dummy", "username" : "duummyy1" "friend_count": 10 "friend_of" "aCgAFAGDIgWRSUu9a2aMo9HtnnD3": true }, 

then a simple deep query will return everything you need. (Firebase v2)

let myUid = "aCgAFAGDIgWRSUu9a2aMo9HtnnD3" let path = ("friend_of/\(myUid)") // equals friend_of/aCgAFAGDIgWRSUu9a2aMo9HtnnD3 var userIdArray = [String]() userProfileRef.queryOrderedByChild(path) .queryEqualToValue(true) .observeSingleEventOfType(.Value, withBlock: { snapshot in for child in snapshot.children { let userId = child.key as String userIdArray.append(userId) } //loop is done, now we have an array of userIds that are friends }) 

The code returns every user who is a friend on mine (myUid)

I've also added a 'friend count' node that simply keeps a count of that users number of friends. When a friend is added, increment the value, when removed decrement. It's a quick

myUid.child("friend_count").setValue(updated_count) 

This code also avoids loops, callbacks and additional completion blocks because it rely's on Firebase to get the data, and let us know when it's done.

If you really, really want to read an array node (not recommended)

 let myRef = self.myRootRef.child("array_node") myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in let a = snapshot.value as! NSArray print(a) //a an NSArray let b = (a as Array).filter {$0 is String} print(b) //b is a Swift Array print( b[1] ) }) 

Array's can be challenging in Firebase as the individual elements cannot be accessed directly or modified. You either read the whole array or write the whole array.

I would suggest changing the Structure to better match the data you want and avoid array's entirely:

"UserProfile" : { "D8QmnOSH6vRYiMujKNXngzhdn992" : { "age" : "12", "email" : "[email protected]", "gender" : "f", "password" : "123454321", "typeOfBackpacker" : "dummy", "username" : "duummyy1" "friend_count": 10 "friend_of" "aCgAFAGDIgWRSUu9a2aMo9HtnnD3": true }, 

then a simple deep query will return everything you need.

let myUid = "aCgAFAGDIgWRSUu9a2aMo9HtnnD3" let path = ("friend_of/\(myUid)") // equals friend_of/aCgAFAGDIgWRSUu9a2aMo9HtnnD3 var userIdArray = [String]() userProfileRef.queryOrderedByChild(path) .queryEqualToValue(true) .observeSingleEventOfType(.Value, withBlock: { snapshot in for child in snapshot.children { let userId = child.key as String userIdArray.append(userId) } //loop is done, now we have an array of userIds that are friends }) 

The code returns every user who is a friend on mine (myUid)

I've also added a 'friend count' node that simply keeps a count of that users number of friends. When a friend is added, increment the value, when removed decrement. It's a quick

myUid.child("friend_count").setValue(updated_count) 

This code also avoids loops, callbacks and additional completion blocks because it rely's on Firebase to get the data, and let us know when it's done.

If you really, really want to read an array node (not recommended)

 let myRef = self.myRootRef.child("array_node") myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in let a = snapshot.value as! NSArray print(a) //a an NSArray let b = (a as Array).filter {$0 is String} print(b) //b is a Swift Array print( b[1] ) }) 

Array's can be challenging in Firebase as the individual elements cannot be accessed directly or modified. You either read the whole array or write the whole array.

I would suggest changing the Structure to better match the data you want and avoid array's entirely:

"UserProfile" : { "D8QmnOSH6vRYiMujKNXngzhdn992" : { "age" : "12", "email" : "[email protected]", "gender" : "f", "password" : "123454321", "typeOfBackpacker" : "dummy", "username" : "duummyy1" "friend_count": 10 "friend_of" "aCgAFAGDIgWRSUu9a2aMo9HtnnD3": true }, 

then a simple deep query will return everything you need (Firebase v2)

let myUid = "aCgAFAGDIgWRSUu9a2aMo9HtnnD3" let path = ("friend_of/\(myUid)") // equals friend_of/aCgAFAGDIgWRSUu9a2aMo9HtnnD3 var userIdArray = [String]() userProfileRef.queryOrderedByChild(path) .queryEqualToValue(true) .observeSingleEventOfType(.Value, withBlock: { snapshot in for child in snapshot.children { let userId = child.key as String userIdArray.append(userId) } //loop is done, now we have an array of userIds that are friends }) 

The code returns every user who is a friend on mine (myUid)

I've also added a 'friend count' node that simply keeps a count of that users number of friends. When a friend is added, increment the value, when removed decrement. It's a quick

myUid.child("friend_count").setValue(updated_count) 

This code also avoids loops, callbacks and additional completion blocks because it rely's on Firebase to get the data, and let us know when it's done.

If you really, really want to read an array node (not recommended)

 let myRef = self.myRootRef.child("array_node") myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in let a = snapshot.value as! NSArray print(a) //a an NSArray let b = (a as Array).filter {$0 is String} print(b) //b is a Swift Array print( b[1] ) }) 
Source Link
Jay
  • 35.8k
  • 19
  • 61
  • 89

Array's can be challenging in Firebase as the individual elements cannot be accessed directly or modified. You either read the whole array or write the whole array.

I would suggest changing the Structure to better match the data you want and avoid array's entirely:

"UserProfile" : { "D8QmnOSH6vRYiMujKNXngzhdn992" : { "age" : "12", "email" : "[email protected]", "gender" : "f", "password" : "123454321", "typeOfBackpacker" : "dummy", "username" : "duummyy1" "friend_count": 10 "friend_of" "aCgAFAGDIgWRSUu9a2aMo9HtnnD3": true }, 

then a simple deep query will return everything you need.

let myUid = "aCgAFAGDIgWRSUu9a2aMo9HtnnD3" let path = ("friend_of/\(myUid)") // equals friend_of/aCgAFAGDIgWRSUu9a2aMo9HtnnD3 var userIdArray = [String]() userProfileRef.queryOrderedByChild(path) .queryEqualToValue(true) .observeSingleEventOfType(.Value, withBlock: { snapshot in for child in snapshot.children { let userId = child.key as String userIdArray.append(userId) } //loop is done, now we have an array of userIds that are friends }) 

The code returns every user who is a friend on mine (myUid)

I've also added a 'friend count' node that simply keeps a count of that users number of friends. When a friend is added, increment the value, when removed decrement. It's a quick

myUid.child("friend_count").setValue(updated_count) 

This code also avoids loops, callbacks and additional completion blocks because it rely's on Firebase to get the data, and let us know when it's done.

If you really, really want to read an array node (not recommended)

 let myRef = self.myRootRef.child("array_node") myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in let a = snapshot.value as! NSArray print(a) //a an NSArray let b = (a as Array).filter {$0 is String} print(b) //b is a Swift Array print( b[1] ) })