Firebase Database Not Equal Request - Alternative Solution (For iOS)

Firebase database Not equal request - Alternative solution (for iOS)

The first solution is to load the comments via .ChildAdded and ignore the ones with the current user_id

let commentsRef = self.myRootRef.childByAppendingPath("comments")

commentsRef.observeEventType(.ChildAdded, withBlock: { snapshot in

let uid = snapshot.value["uid"] as! String
if uid != current_uid {
//do stuff
}
})

You could expand on this and load everything by .Value and iterate over the children in code as well. That method will depend on how many nodes you are loading - .ChildAdded will be lower memory usage.

Retrieving data from Firebase equals unexpected nil - swift

Since in the second code you are directly getting DataSnapshot of your Post, I don't think you need to iterate the snapshot.

Try changing your code to this, see if it works.

func observePosts(userID: String) {
let ref = FIRDatabase.database().reference().child("Users").child(userID).child("Wall")
ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in

let postId = snapshot.key
let postReference = FIRDatabase.database().reference().child("feed-items").child(postId)

postReference.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
print(snapshot)
let update = Sweet(snapshot: snapshot)
self.updates.insert(update, atIndex: 0)

dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})

}, withCancelBlock: nil)

}, withCancelBlock: nil)
}

swift 3 firebase snapshot if value equal to ... fetch

You're missing the value in your query:

Database.database().reference()
.child("rooms")
.queryOrdered(byChild: "Owner")
.queryEqual(toValue: "STbz...")
.observeSingleEvent(of: .value, with: { (snapshot) in

See for this and more query operators, the documentation on filtering data.

How to safely handle multiple writes in firebase which must all happen

So I stole this from the Firebase blog and got it to match my code. The answer is fairly intuitive, I just hadn't considered it. Basically you just create a reference to the top level of your database and specify the paths you want to write to in the dictionary (so not by creating specific references with child()), and then just call updateChildValues().

static func sendRequestFromCurrentUser(toUser userThatRequestWasSentTo : User, succeeded : @escaping (Bool)->Void ){

let ref = Database.database().reference()

// Create the data we want to update
var updatedUserData : [String : Any] = [:]
updatedUserData["users/\(User.current.uid)/sentRequests/\(userThatRequestWasSentTo.uid)"] = userThatRequestWasSentTo.toDictionary()
updatedUserData["users/\(userThatRequestWasSentTo.uid)/receivedRequests/\(User.current.uid)"] = User.current.toDictionary()

// Do a deep-path update
ref.updateChildValues(updatedUserData, withCompletionBlock: { (error, ref) in
if let error = error {
print("Error updating data: \(error.localizedDescription)")
succeeded(false)
}
else{
succeeded(true)
}
})
}

Swift Firebase Realtime Database data not pulled before end of function, data not shown in view

The issue is in with your getFriends method. In your code observeSingleEvent is an asynchronous function, so call completion(friends) only inside that closure, it will fix the issue. Change the method as below.

func getFriends(completion: @escaping ([Friend])-> Void) {
print("start getFriends")
self.friends = []
let ref = Database.database().reference(fromURL: "users")

ref.queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
print("entering data snapshot pull")
let snapshotValue = snapshot.value as! [String:[String:[String:AnyObject]]]

for (key, _) in snapshotValue {
for (_, value) in snapshotValue[key]! {
var email = ""
var userName = ""
var id = ""
for (key, value) in value {
switch key{
case "email":
email = value as? String ?? ""
case "userName":
userName = value as? String ?? ""
case "id":
id = value as? String ?? ""
default:
print("")
}
}
print("--------------------------")
print(userName)
print(email)
print(id)
self.friends.append(Friend(userName: userName, email: email, id: id))
}
}
print("leaving data snapshot pull")
// CHANGE -----
completion(friends)
}
print("end getFriends")

}


Related Topics



Leave a reply



Submit