Я получаю эту ошибку при запуске приложения.Поток 1: неустранимая ошибка: неожиданно обнаружен ноль при развертывании необязательного значения - PullRequest
0 голосов
/ 23 ноября 2018

Всякий раз, когда я запускаю свое приложение, оно всегда падает в любом месте, где указано currentUserID.Не уверен почему.Я напечатал значение currentUserID и его ноль.Как я могу исправить это, чтобы он хорошо работал мое приложение.Я могу поделиться остальным кодом при необходимости.

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)

                            }
                        }
                    }
                })

1 Ответ

0 голосов
/ 24 ноября 2018

Есть несколько проблем, которые необходимо устранить, а затем - основная проблема.

Первое, что Auth.auth (). CurrentUser?является необязательным - это означает, что он может иметь значение или может быть ноль.Делая это ..

let currentUserId = Auth.auth().currentUser?.uid

сообщает вашему коду, что независимо от того, получил ли он значение или ноль, попытайтесь присвоить .uid для currentUserId.

Защитите свой код, безопасно развернув параметры

guard let currentUser = Auth.auth().currentUser else {return}
//now you can access currentUser as it won't be nil

или

if let currentUser = Auth.auth().currentUser {
   print("user is authenticated and currentUser is not nil")
} else {
   print("not authenticated")
}

Теперь суть проблемы, насколько я понимаю, заключается в том, что вы вообще не аутентифицируете пользователя, и это должно произойти доДоступ к Auth.auth (). currentUser

Вот полная функция аутентификации с обработкой ошибок

func authUser(user: String, pw: String) {
    Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
        if let x = error {
            let err = x as NSError
            switch err.code {
            case AuthErrorCode.wrongPassword.rawValue:
                print("wrong password")
            case AuthErrorCode.invalidEmail.rawValue:
                print("invalued email")
            default:
                print("unknown error")
            }
        } else {
            if let user = auth?.user {  //note; safely unwrap optional
                print("uid: \(user.uid)")
            }
        }
    })
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...