Whenever I run my app, it always crashes anywhere it states currentUserID. Not sure why. I printed the the currentUserID value and its nil. How can I correct it so it runs my app well. I can share the rest of the code if needed.
import UIKit
import MapKit
import CoreLocation
import Firebase
//import FirebaseDatabase
enum AnnotationType {
case pickup
case destination
case driver
}
enum ButtonAction {
case requestRide
case getDirectionsToPassenger
case getDirectionToDestination
case startTrip
case endTrip
}
class HomeVC: UIViewController, Alertable {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var centerMapBtn: UIButton!
@IBOutlet weak var destinationTextField: UITextField!
@IBOutlet weak var destinationCircle: UIView!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var actionBtn: UIButton!
var delegate: CenterVCDelegate?
var manager: CLLocationManager?
let currentUserId = Auth.auth().currentUser?.uid
// "IRD70YtgEyWRpaH8LkkjHZmjXPo1"
var ref: DatabaseReference!
var regionRadius: CLLocationDistance = 1000
var tableView = UITableView()
var matchingItems: [MKMapItem] = [MKMapItem]()
var route: MKRoute!
var selectedItemPlacemark: MKPlacemark? = nil
var actionForButton: ButtonAction = .requestRide
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
// print(ref.child("users").value(forKey: "users"))
self.ref.child("userse").childByAutoId().setValue("wise")
self.ref.child("users").child("sdsd").setValue("data")
// print(Auth.auth().currentUser?.uid)
// print(currentUserId)
manager = CLLocationManager()
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest
checkLocationAuthStatus()
mapView.delegate = self
destinationTextField.delegate = self
centerMapOnUserLocation()
DataService.instance.REF_DRIVERS.observe(.value, with: { (snapshot) in
self.loadDriverAnnotationFromFB()
DataService.instance.passengerIsOnTrip(passengerKey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
self.zoom(toFitAnnotationsFromMapView: self.mapView, forActiveTripWithDriver: true, withKey: driverKey)
}
})
})
cancelBtn.alpha = 0.0
UpdateService.instance.observeTrips { (tripDict) in
if let tripDict = tripDict {
let pickupCoordinateArray = tripDict["pickupCoordinate"] as! NSArray
let tripKey = tripDict["passengerKey"] as! String
let acceptanceStatus = tripDict["tripIsAccepted"] as! Bool
if acceptanceStatus == false {
//Broadcast to all driver
DataService.instance.driverIsAvailable(key: self.currentUserId!, handler: { (available) in
//check for errors
if let available = available {
if available == true {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let pickupVC = storyboard.instantiateViewController(withIdentifier: "PickupVC") as? PickupVC
pickupVC?.initData(coordinate: CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees), passengerKey: tripKey)
self.present(pickupVC!, animated: true, completion: nil)
}
}
})
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.currentUserId == nil {
print("login")
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (status) in
if status == true {
self.buttonsForDriver(areHidden: true)
}
})
DataService.instance.REF_TRIPS.observe(.childRemoved, with: { (removedTripSnapshot) in
let removedTripDict = removedTripSnapshot.value as? [String: AnyObject]
if removedTripDict?["driverKey"] != nil {
DataService.instance.REF_DRIVERS.child(removedTripDict?["driverKey"] as! String).updateChildValues(["driverIsOnTrip": false])
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (isDriver) in
if isDriver == true {
// Remove Overlays and annotation and hide request ride and cancel button
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
} else {
self.cancelBtn.fadeTo(alphaValue: 0.0, withDuration: 0.2)
self.destinationTextField.isUserInteractionEnabled = true
self.destinationTextField.text = ""
// Remove all map annotation and overlays
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
self.centerMapOnUserLocation()
}
})
})
DataService.instance.driverIsOnTrip(driverkey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
DataService.instance.REF_TRIPS.observeSingleEvent(of: .value, with: { (tripSnapshot) in
if let tripSnapshot = tripSnapshot.children.allObjects as? [DataSnapshot] {
for trip in tripSnapshot {
if trip.childSnapshot(forPath: "driverKey").value as? String == self.currentUserId! {
let pickupCoordinateArray = trip.childSnapshot(forPath: "pickupCoordinate").value as! NSArray
let pickupCoordinate = CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees)
let pickupPlacemark = MKPlacemark(coordinate: pickupCoordinate)
self.dropPinFor(placemark: pickupPlacemark)
self.searchMapKitForResultsWithPolyline(forOriginMapItem: nil, withDestinationMapItem: MKMapItem(placemark: pickupPlacemark))
self.setCustomRegion(forAnnotationType: .pickup, withCoordinate: pickupCoordinate)
self.actionForButton = .getDirectionsToPassenger
self.actionBtn.setTitle("GET DIRECTION", for: .normal)
// Fade in the action button
self.buttonsForDriver(areHidden: false)
}
}
}
})