Расположение ядра в MVP

Вам нужно использовать Popen и subprocess.PIPE , чтобы поймать выход процесса, когда истекает время ожидания. В частности, Popen.communicate - это то, что вам нужно. Вот пример

proc = subprocess.Popen(["ping", "192.168.1.1"],
                        stdout=subprocess.PIPE)

try:
    output, error = proc.communicate(timeout=2)
except subprocess.TimeoutExpired:
    proc.kill()
    output, error = proc.communicate()
    print(output)
    print(error)

. Это будет печатать выход процесса до истечения времени ожидания.

0
задан Alina Vas 21 March 2019 в 22:31
поделиться

1 ответ

Вы можете использовать Делегат , чтобы уведомить WeatherPresenter об изменениях от LocationService

protocol LocationServiceDelegate: class { // Delegate protocol
    func didUpdateLocation()
}

class LocationService: NSObject, CLLocationManagerDelegate {

    weak var delegate: LocationServiceDelegate?

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func startUpdatingLocation() { // Start updating called from presenter
        locationManager.startUpdatingLocation()
    }

    func getCurrentLocation() -> Location? {
        // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
                self.delegate?.didUpdateLocation() // Notify delegate on change
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

}


class WeatherPresenter: LocationServiceDelegate {
    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
        self.locationService.delegate = self // Set self as delegate
        self.locationService.startUpdatingLocation()  // Requests start updating location
    }

    func didUpdateLocation() { // This will be called on location change
        self.getWeatherForCurrentLocation()
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}
0
ответ дан MCMatan 21 March 2019 в 22:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: