0

Trying to find out why sometimes this happens in swift/Xcode. When I check "employees.isEmpty" is returns correctly because it is empty. When is check "uids.isEmpty" it return empty as well but for some reason it thinks there are values and then executes my firebase call which of course crashes the app because the array is nil. Why does this happen? Same code. Same set up. Yet "employees.isEmpty" returns nil like it should (in my testing) and the other "uids.isEmpty" is empty but executes like there are values when there are not.

if employees.isEmpty == false {
    print("This shouldnt be called 1")
    OneSignal.postNotification(["contents": ["en": "\(message)"],
                                "include_player_ids": employees,
                                "headings": ["en": "Hey \(businessName)"],
                                "ios_badgeType": "Increase",
                                "ios_badgeCount": 1])
} else {
    print("Empty no execution")
}
print(employees)
print("Checking inside employees here")
print(uids)
print("Checking inside uids here")

if uids.isEmpty == false {
    print("This shouldnt be called 2")
    let pathing = ref.child("notification").child(uids).childByAutoId()
    pathing.setValue(values)
} else {
    print("Empty no execution")
}

They both print out the same when no values are assigned, just like this: "[]".

Declarations/Appending

var data:[String] = []
var dataUID:[String] = []


// GETTING THE ONESIGNAL ID'S FROM THE EMPLOYEES FOR NOTIFICATIONS
func getEmployees() {
    Database.database().reference().child("Businesses").child(self.otherUser?["uid"] as! String).child("registered_employees").observe(.childAdded, with: { (snapshot) in
        if snapshot.exists() {
           let data = snapshot.value as? NSDictionary
            let uid = data?["onesignal"] as? String
            self.data.append(uid!)
            print(self.data)
        } else {
            print("didnt call right values")
        }
    })
}

func getEmployeesUIDs() {
    Database.database().reference().child("Businesses").child(self.otherUser?["uid"] as! String).child("registered_employees").observe(.value, with: { (snapshot) in
        if snapshot.exists() {
            let data = snapshot.value as? NSDictionary
            let uid = data?["uid"] as? String
            self.dataUID.append(uid!)
            print(self.dataUID)
            print("Printing the employee uids right here")
        } else {
            print("didnt call right values")
        }
    })
}


let employees = self.data
let uids = self.dataUID.description

if employees.isEmpty {
                print("Empty no execution")
            } else {
                print("This shouldnt be called 1")
                OneSignal.postNotification(["contents": ["en": "\(message)"],
                                            "include_player_ids": employees,
                                            "headings": ["en": "Hey \(businessName)"],
                                            "ios_badgeType": "Increase",
                                            "ios_badgeCount": 1])
            }
            print(employees)
            print("Checking inside employees here")
            print(uids)
            print("Checking inside uids here")

            if uids.isEmpty {
                print("Empty no execution")
            } else {
                print("This shouldnt be called 2")
                let pathing = ref.child("notification").child(uids).childByAutoId()
                pathing.setValue(values)

            }
11
  • 2
    This is probably not relevant, but never say uids.isEmpty == false. isEmpty is already a Bool; do not compare it to something else. Just say if !uids.isEmpty. Commented Apr 18, 2019 at 15:54
  • how do you init employees and uids? Commented Apr 18, 2019 at 15:55
  • 1
    As for actual question, it would probably help us to know how employees and uids are declared and how / when they are populated. You say the array is nil; that suggests it is an Optional. That could be important to know. And it might be that you are populating these arrays asynchronously, in which case there may be a code-out-of-order issue that you are not showing us yet. Commented Apr 18, 2019 at 15:56
  • Added the extra code Commented Apr 18, 2019 at 15:59
  • 1
    You are checking inside a String in case of uids, not an Array. Commented Apr 18, 2019 at 16:19

1 Answer 1

1
if employees.isEmpty == false {
    print("This shouldnt be called 1")
    OneSignal.postNotification(["contents": ["en": "\(message)"],
                                "include_player_ids": employees,
                                "headings": ["en": "Hey \(businessName)"],
                                "ios_badgeType": "Increase",
                                "ios_badgeCount": 1])
} else {
    print("Empty no execution")

    print(employees)
    print("Checking inside employees here")
    print(uids)
    print("Checking inside uids here")}

    if uids.isEmpty == false {
        print("This shouldnt be called 2")
        let pathing = ref.child("notification").child(uids).childByAutoId()
        pathing.setValue(values)
    } else {
        print("Empty no execution")
    }

Try the above code. I have moved the part where you show the array in else so that if array is nil it will show.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.