How to use Nil in control flow with Firebase snapshots? (Swift) [duplicate] - ios

This question already has answers here:
Checking if Firebase snapshot is equal to nil in Swift
(2 answers)
Closed 6 years ago.
I am trying to check to see if a key/value (TestKey/TestValue) exists at a node and if it does do A and if it does not do B.
I have found this similar thread regarding this on StackOverflow but none of the solutions seemed to work for me: Checking if Firebase snapshot is equal to nil in Swift
I have a database set up that already has the TestKey/TestValue key pair inserted.
Here is my code:
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//A print test to see stored value before control flow
print(snapshot.value!["TestKey"]) //returns TestValue
print(snapshot.value!["NilKey"]) //returns nil
// I change the value from TestKey <-> NilKey here to test the control flow
if snapshot.value!["TestKey"] != nil {
print("Exists currently")
} else {
print("Does not exist currently")
}
}) { (error) in
print(error.localizedDescription)
}
The problem is that only "A" is executed, even if the print test shows that the result is nil.
I have tried to reverse the operations with == nil and also have tried "is NSNull".
I cannot seem to get the control flow to work properly. Maybe one of the following is the culprit but I am unsure:
interaction of persistence with observesSingleEventOfType
not removing the observer (do I even need to for observeSingleEventType?)
something to do with optionals
something to do with asynchronous calls
something to do with the fact that firebase stores NSAnyObjects
Ultimately the goal is to check if a key/value pair exists and if it does exist: do nothing but if it does not exist: create it.
What am I doing wrong and how can I get this control flow to work?

Ok so after much fiddling I found a solution that works, it was indeed posted as this answer (https://stackoverflow.com/a/35682760/6515861) from the earlier existing question I linked (Checking if Firebase snapshot is equal to nil in Swift).
The way to make it work is to input the TestKey in the reference itself, remove the reference from snapshot.value("TestKey"), and then use "is Null".
Here is the working code:
// Database already exists with "TestKey"/"TestValue"
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
// replace "TestKey" with another string to test for nil
ref.child("TestKey").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if snapshot.value is NSNull {
print("Does not exist currently")
} else {
print("Exists currently")
}
}) { (error) in
print(error.localizedDescription)
}
I am happy I found this solution but if anyone wants to still answer the original question I would love to know why it does not work the other ways.

Related

Way/s to determine table does not exist in firebase [duplicate]

This question already has answers here:
How do I check if a firebase database value exists?
(6 answers)
Closed 4 years ago.
I need to get all the data from a specific table but their are times that the table does not exist in firebase. And I want to know if the reason is the table doesn't exist.
I'm using observe(.childAdded) btw.
Try this if this will work
let ref = Database.database().reference()
ref.observeSingleEvent(of: .value) { (snapshot) in
if snapshot.hasChild("mytable") {
// exist
} else {
// does not exist
}
}
While Elbert's answer works, it downloads more data than needed. It downloads your entire database, to check if one node exists.
I recommend instead to only read the node that you're checking:
let ref = Database.database().reference()
ref.child("mytable").observeSingleEvent(of: .value) { (snapshot) in
if snapshot.exists() {
// exists
} else {
// does not exist
}
}
Also see:
Swift Firebase Check if Value Exists
How do I check if a firebase database value exists?

Value of String won't change in Swift despite clear declaration [duplicate]

This question already has answers here:
Return Statement Error using Firebase in Swift
(1 answer)
Code Not Completing Before Next Method Is Called
(1 answer)
Swift accessing response from function
(1 answer)
Closed 4 years ago.
I am making an app in which I have to return a String representing the existence of a certain reference to my firebase realtime database (I am not using a Bool for other reasons). Despite executing the print commands directly above and below, when I print the function, the value of the String won't change and prints its default value "Unchanged".
My code:
func checkIfMovieHasAlreadyBeenShown(movieID: String) -> String {
var hasItBeenSeen = "Unchanged"
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("Seen Movies").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(movieID){
print("The Result:")
self.changeStringHasItBeenSeen(YNString: "yes")
print("YES")
}
else {
print("The Result:")
self.changeStringHasItBeenSeen(YNString: "no")
print("NO")
}
})
return hasItBeenSeen
}
It is printed here:
print(checkIfMovieHasAlreadyBeenShown(movieID: "39"))
print(checkIfMovieHasAlreadyBeenShown(movieID: "38"))
observeSingleEvent is called in a closure. The return hasItBeenSeen line will not be affected by anything in the closure, as it will return first.
Imagine this: you tell your friend to go buy some apples. Before he has even left, you ask him for the price. Of course, he won't know, because he hasn't gone to buy the apples yet.
You need some way of performing something AFTER the closure has been called (or after the apples have been bought). Usually this can be done with a completion handler, where you pass a closure into your checkIfMovieHasAlreadyBeenShown function, such as, "do this AFTER you buy the apples".
Then you would call your function this way: checkIfMovieHasAlreadyBeenShown(movieID: "39", completion: { seen in print(seen) })

How to wait for firebase to load the data into a variable before returning the variable then call the function [duplicate]

This question already has answers here:
Wait for Firebase to load before returning from a function
(2 answers)
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Closed 5 years ago.
This Question is not a duplicate.
So I have a simple function that will take a parameter of the index of what tableview cell was clicked so the function will go into the didSelectRowAt tableView function, and it will also return a String.
The Function is called getOpposingUserName and it connects to firebase to gather the information provided inside the database.
Now I know that the functionality of the function works it is sort of a logic error that I don't quite understand. From what I know is that whenever you try to get data from firebase it runs that code and takes a second to actually get the data. but as its doing that it keeps running the code below where you tried to get data from the database.
which means if I want to return a String then the string will always be nil or whatever I set its initial value to, because the database will take to long to load and the function will already have ran through the code to return something.(that's my understanding of it, I'm sure there is a way more technical way to explain it)
so basically what I am asking is how would I "load" the data into a variable first then check that firebase has indeed loaded something into the variable and then return the variable for whatever called the function
This question IS not a duplicate I have read the question and tried to make sense of it in my own implementation. I am not returning a function that code is confusing to me when I read it.
here is my code for the function
func getOpposingUsername(_ index: Int) -> String {
var opposingUser: String = ""
//the self.tieBetToUser just gets the randomized id from the bet in firebase don't need to worry about it
self.datRef.child("Bets").child(self.tieBetToUser[index]).observe(.childAdded, with: { snapshot in
guard let dict = snapshot.value as? [String: AnyHashable] else {
return
}
opposingUser = dict["OpposingUsername"] as! String
print(opposingUser) //this code actually never runs I found out
})
//sleep(4) this will not work and is actually bad code anyway different phones may be faster or slower
return opposingUser // will always return "" at the moment
}
here is the solution the internet has provided
typealias someting = (String?) -> Void
func getOpposingUsername(completionHandler: #escaping someting, _ index: Int) {
var opposingUser: String = ""
self.datRef.child("Bets").child(self.tieBetToUser[index]).observe(.childAdded, with: { snapshot in
guard let dict = snapshot.value as? [String: AnyHashable] else {
return
}
opposingUser = dict["OpposingUsername"] as! String
if opposingUser.isEmpty {
completionHandler(nil)
} else {
completionHandler(opposingUser)
}
})
}
How exactly would I call this function somewhere else? like
getOpposingUsername(somethingGoesHere, index.path)

How to Update uitableview from CloudKit using discoverAllIdentities

So, I am new to cloudKit and to working with multiple threads in general, which I think is the source of the problem here, so if I simply need to research more, please just comment so and I will take that to heart.
Here is my question:
I am working in Swift 3 Xcode 8.1
I have in my view controller this variable:
var contactsNearby: [String:CLLocation]?
Then at the end of ViewDidLoad I call one of my view controllers methods let's call it:
populateContactsNearby()
inside that method I call:
container.discoverAllIdentities(completionHandler: { (identities, error) in
for userIdentity in identities! {
self.container.publicCloudDatabase.fetch(withRecordID: userIdentity.userRecordID!, completionHandler: { (userRecord, error) in
let contactsLocation = userRecord?.object(forKey: "currentLocation")
if self.closeEnough(self.myLocation!, contactLocation: contactsLocation as! CLLocation) {
var contactsName = ""
contactsFirstName = userIdentity.nameComponents?.givenName
if contactsName != "" && contactsLocation != nil {
self.contactsNearby?["\(contactsName)"] = contactsLocation as? CLLocation
}
}
})
}
})
}
I apologize if I am missing or have an extra bracket somewhere. I have omitted some error checking code and so forth in order to get this down to bare-bones. So the goal of all that is to populate my contactsNearby Dictionary with data from CloudKit. A name as the key a location as the value. I want to use that data to populate a tableview. In the above code, the call to closeEnough is a call to another one of my view controllers methods to check if the contact from CloudKit has a location close enough to my user to be relevant to the apps purposes. Also myLocation is a variable that is populated before the segue. It holds the CLLocation of the app users current location.
The Problem:
The if statement:
if contactsName != "" && contactsLocation != nil { }
Appears to succeed. But my view controllers variable:
var contactsNearby: [String:CLLocation]?
Is never populated and I know there is data available in cloudKit.
If it's relevant here is some test code that I have in cellForRowAtIndexPath right now:
let contact = self.contactsNearby?.popFirst()
let name = contact?.key
if name != nil {
cell.textLabel?.text = name
}else {
cell.textLabel?.text = "nothing was there"
}
My rows alway populate with "nothing was there". I have seen answers where people have done CKQueries to update the UI, but in those answers, the user built the query themselves. That seems different from using a CloudKit function like discoverAllIdentities.
I have tried to be as specific as possible in asking this question. If this question could be improved please let me know. I think it's a question that could benefit the community.
Okay, I need to do some more testing, but I think I got it working. Thank you Paulw11 for your comment. It got me on the right track.
As it turns out there were 2 problems.
First, as pointed out I have an asynchronous call inside a for loop. As recommended, I used a dispatchGroup.
Inside the cloudKit call to discoverAllIdentities I declared a dispatchGroup, kind of like so:
var icloudDispatchGroup = DispatchGroup()
Then just inside the for loop that is going to make an async call, I enter the dispatchGroup:
icloudDispatchGroup.enter()
Then just before the end of the publicCloudDatabase.fetch completion handler I call:
icloudDispatchGroup.leave()
and
icloudDispatchGroup.wait()
Which, I believe, I'm still new to this remember, ends the dispatchGroup and causes the current thread to wait until that dispatchGroup finishes before allowing the current thread to continue.
The Above took care of the multithreading issue, but my contactsNearby[String:CLLocation]? Dictionary was still not being populated.
Which leads me to the 2nd problem
At the top of my view controller I declared my Dictionary:
var contactsNearby: [String: CLLocation]?
This declared a dictionary, but does not initialize it, which I don't think I fully realized, so when I attempted to populate it:
self.contactsNearby?["\(contactsName)"] = contactsLocation as? CLLocation
It quietly failed because it is optional and returned nil
So, in viewDidLoad before I even call populateContactsNearby I initialize the dictionary:
contactsNearby = [String:CLLocation]()
This does not make it cease to be an optional, which Swift being strongly typed would not allow, but merely initializes contactsNearby as an optional empty Dictionary.
At least, that is my understanding of what is going on. If anyone has a more elegant solution, I am always trying to improve.
In case you are wondering how I then update the UI, I do so with a property observer on the contactsNearby Dictionary. So the declaration of the dictionary at the top of the view controller looks like this:
var contactsNearby: [String: CLLocation]? {
didSet {
if (contactsNearby?.isEmpty)! || contactsNearby == nil {
return
}else{
DispatchQueue.main.sync {
self.nearbyTableView.reloadData()
}
}
}
}
I suppose I didn't really need to check for empty and nil. So then in cellForRowAtIndexPath I have something kind of like so:
let cell = tableview.dequeueReusableCell(withIdentifier: "nearbyCell", for: indexPath)
if contactsNearby?.isEmpty == false {
let contact = contactsNearby?.popFirst()
cell.textLabel?.text = contact?.key
}else {
cell.textLabel?.text = "Some Placeholder Text Here"
}
return cell
If anyone sees an error in my thinking or sees any of this heading for disaster, feel free to let me know. I still have a lot of testing to do, but I wanted to get back here and let you know what I have found.

Swift - Variable doesn't retain value

I am using AWS to host images for my iOS app. Right now I am trying to list all the objects in an S3 bucket.
Here is my code:
var description = ""
AWSS3.registerS3WithConfiguration(serviceConfig2, forKey: "s3")
AWSS3.S3ForKey("s3").listObjects(objectList).continueWithBlock { (task: AWSTask!) -> AnyObject! in
if task.error != nil {
println(task.error)
}
if task.result != nil {
description = task.result!.description
println(description)
}
return nil
}
println(description == "")
The output is true followed by the correct contents of task.result!.description. In other words, the println outside of the continueWithBlock is printing first and description has not been updated at that time.
How am I supposed to do things with description outside of the continueWithBlock?
You can assign a value that you need to another variable inside the scope of your class or function, then you can call didSet on the variable and carry out another function if you need to, like this:
var someVariableInScopeOfWhereItsNeeded = "abc" {
didSet {
self.maybeSomeOtherFunctionNow
}
}
You asked:
How am I supposed to do things with description outside of the
continueWithBlock
Short answer: You're not.
The whole point of an async method is that it continues immediately, before the time-consuming task has even begun processing. You put the code that depends on the results inside your block. See my answer on this thread for a detailed explanation, including an example project:
Why does Microsoft Azure (or Swift in general) fail to update a variable to return after a table query?
(Don't be fooled by the fact that it mentions MS Azure. It actually has nothing to do with Azure.)
#thefredelement 's solution of using a didSet method on the variable that gets set would work too.

Resources