1

I'm working on a weather and news app, and when I run the app on my device, it says "This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationManagerDidChangeAuthorization: callback and checking authorizationStatus first." I am getting the error, what should I do? This error message appears on two lines with the CLLocationManager.locationServicesEnabled() else { method. I have added the codes of the page where I got the error below. I am using openweathermap API as weather API and newsapi API as news API.

import Foundation
import CoreLocation
import Combine

typealias LocationNameResultType = Result<String, Error>

class WeatherService: WeatherServiceProtocol {
    
    private let apiProvider = APIProvider<WeatherEndpoint>()

    private let locationManager = CLLocationManager()
    
    private lazy var location: CLLocation? = locationManager.location
    
    init() {
        startUpdatingLocation()
    }
    
    func getCityName(completion: @escaping (LocationNameResultType) -> Void) {
        guard let location = location else {
            completion(.failure(WeatherServiceErrors.locationNil))
            return
        }
        
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
            if let error = error {
                completion(.failure(error))
            }
            
            guard let placemark = placemarks?.first,
                let cityName = placemark.locality else {
                completion(.failure(WeatherServiceErrors.placeMarkNil))
                return
            }
            
            completion(.success(cityName))
        }
    }
    
    func requestCurrentWeather() -> AnyPublisher<Data, Error> {
        locationManager.requestWhenInUseAuthorization()
        
        guard CLLocationManager.locationServicesEnabled() else {
            return Fail(error: WeatherServiceErrors.userDeniedWhenInUseAuthorization)
                .eraseToAnyPublisher()
        }
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
        
        guard let location = location else {
            return Fail(error: WeatherServiceErrors.locationNil)
                .eraseToAnyPublisher()
        }
        
        return apiProvider.getData(
            from: .getCurrentWeather(latitude: location.coordinate.latitude,
                                     longitude: location.coordinate.longitude)
        )
            .eraseToAnyPublisher()
    }
    
    deinit {
        stopUpdatingLocation()
    }
}

private extension WeatherService {
    func startUpdatingLocation() {
        locationManager.requestWhenInUseAuthorization()
        
        guard CLLocationManager.locationServicesEnabled() else {
            return
        }
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    
    func stopUpdatingLocation() {
        locationManager.stopUpdatingLocation()
    }
}

When I want to check the weather, the application gives the error I mentioned above during the test via Xcode and the page does not load.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • Most of your location handling code is incorrect. See [my answer](https://stackoverflow.com/a/74915706/20287183) for the more correct way to setup and handle location updates and authorization. – HangarRash Jun 18 '23 at 16:16

0 Answers0