Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.71 KB | None | 0 0
  1. protocol ReverseGeocodingHelper {
  2.     typealias CountryComparisonCompletion = (Bool, [Error]?) -> ()
  3.     func checkIf(location firstLocation:CLLocation, inTheSameCountryAs secondLocation:CLLocation, completion:@escaping CountryComparisonCompletion)
  4. }
  5. class CoreLocationReverseGeocodingHelper : ReverseGeocodingHelper{
  6.    
  7. //    typealias CountryComparisonCompletion = (Bool, [Error]?) -> ()
  8.    
  9.     func checkIf(location firstLocation:CLLocation, inTheSameCountryAs secondLocation:CLLocation, completion:@escaping CountryComparisonCompletion){
  10.  
  11.         //Declare variables that we would use after
  12.         var firstLocationCountry : String?
  13.         var secondLocationCountry : String?
  14.         var errorInFirstLocationReverseGeocodingRequest : Error?
  15.         var errorInSecondLocationReverseGeocodingRequest : Error?
  16.        
  17.         //Declare the dispatch group
  18.         let dispatchGroup = DispatchGroup()
  19.        
  20.         //Declare nested function for each of the locations
  21.         func reverseGeocodeFirstLocation(){
  22.             dispatchGroup.enter()
  23.             CLGeocoder().reverseGeocodeLocation(firstLocation) { (placemarks, error) in
  24.                 firstLocationCountry = placemarks?.first?.country
  25.                 errorInFirstLocationReverseGeocodingRequest = error
  26.                 dispatchGroup.leave()
  27.             }
  28.         }
  29.         func reverseGeocodeSecondLocation(){
  30.             dispatchGroup.enter()
  31.             CLGeocoder().reverseGeocodeLocation(secondLocation) { (placemarks, error) in
  32.                 secondLocationCountry = placemarks?.first?.country
  33.                 errorInFirstLocationReverseGeocodingRequest = error
  34.                 dispatchGroup.leave()
  35.             }
  36.         }
  37.        
  38.        
  39.         //Do the work in a dispatch group
  40.         //Both functions enter the same dispatch group, so call both
  41.         reverseGeocodeFirstLocation()
  42.         reverseGeocodeSecondLocation()
  43.        
  44.         let queue:DispatchQueue = DispatchQueue.global(qos: .userInteractive)
  45.         dispatchGroup.notify(queue: queue) {
  46.             //Completion will be called only after both items reverse geocoding finished
  47.             let errors : [Error] = [errorInFirstLocationReverseGeocodingRequest,errorInSecondLocationReverseGeocodingRequest].compactMap{$0} //Map only non nil to the array
  48.             if errors.count > 0{
  49.                 completion(false,errors)
  50.             }else{
  51.                 print("First locations is in:\(firstLocationCountry!)\n Second locations is in:\(secondLocationCountry!)")
  52.                 let bothLocationsInSameCountry : Bool = firstLocationCountry! == secondLocationCountry!
  53.                 completion(bothLocationsInSameCountry,nil)
  54.             }
  55.         }
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement