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.