{"__v":17,"_id":"567401b522cd7e0d00fada86","category":{"__v":3,"_id":"5674017bf79ca90d00ad2f67","pages":["56740186f79ca90d00ad2f68","5674019122cd7e0d00fada83","567401b522cd7e0d00fada86"],"project":"5672fc989996590d00c22c65","version":"5672fc989996590d00c22c68","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-18T12:52:11.346Z","from_sync":false,"order":3,"slug":"ios-only-operations","title":"iOS only Operations"},"project":"5672fc989996590d00c22c65","user":"5654ea8be0d82b0d00ab5747","version":{"__v":7,"_id":"5672fc989996590d00c22c68","project":"5672fc989996590d00c22c65","createdAt":"2015-12-17T18:19:04.699Z","releaseDate":"2015-12-17T18:19:04.699Z","categories":["5672fc999996590d00c22c69","567301169d4c060d009da8b3","56730183547bee0d00997d1a","5673018a06b19d0d0010691b","567301b53054630d00fe9288","567400638565060d009a86fb","5674017bf79ca90d00ad2f67"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"2.5.0","version":"2.5.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-18T12:53:09.836Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"Operations has support for location services on iOS.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"User Location\"\n}\n[/block]\n`UserLocationOperation` can retrieve the location of the user's device. Internally it adds an `AuthorizedFor` condition using `Capability.Location`. See the section on [Capabilities](doc:capabilities) for more detailed information, but for now, it's enough to know that the operation will handle authorization for location services automatically.\n\nThe class can be used with a completion block as in the example below\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let operation = UserLocationOperation { location in\\n    print(\\\"Location: \\\\(location)\\\")\\n}\\nqueue.addOperation(operation)\",\n      \"language\": \"swift\",\n      \"name\": \"user location with completion block\"\n    }\n  ]\n}\n[/block]\nAlternatively, `UserLocationOperation` sets the resultant `CLLocation` as a property: `location`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let operation = UserLocationOperation()\\noperation.addObserver(FinishedObserver { op in\\n    if let op = op as? UserLocationOperation {\\n        print(\\\"Location: \\\\(op.location)\\\")    \\n    }\\n})\\nqueue.addOperation(operation)\",\n      \"language\": \"swift\",\n      \"name\": \"accessing property of user location\"\n    }\n  ]\n}\n[/block]\nThe above example is little redundant. A more likely usage of the `location` property would be when the operation is used inside of a `GroupOperation` subclass. See [Groups](doc:groups) for more guidance on creating groups.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Reverse Geocode\"\n}\n[/block]\nCore Location supports [geocoding](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/index.html#//apple_ref/occ/cl/CLGeocoder) functionality. Reverse geocoding is where a known location is looked up to retrieve human readable location information such as an address. The actual result type for this information is a `CLPlacemark`.\n\nIn Operations, this can be used with a `ReverseGeocodeOperation`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let operation = ReverseGeocodeOperation(location: aLocation) { placemark in\\n    print(\\\"Placemark: \\\\(placemark)\\\")\\n}\\nqueue.addOperation(operation)\",\n      \"language\": \"swift\",\n      \"name\": \"reverse geocode\"\n    }\n  ]\n}\n[/block]\nIt also stores the received placemark in a property similar to `UserLocationOperation`.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Reverse Geocode User Location\"\n}\n[/block]\nPutting this all together, we have `ReverseGeocodeUserLocation` which first get the device's current location and then use that for a reverse geocode lookup. Therefore, if you functionality in your app where the user's can enter their current address, it might make sense to use this operation. The returned `CLPlacemark` can return a localized address information.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let operation = ReverseGeocodeUserLocation { location, placemark in\\n    print(\\\"Placemark: \\\\(placemark)\\\")\\n}\\nqueue.addOperation(operation)\",\n      \"language\": \"swift\",\n      \"name\": \"reverse geocode user location\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Accuracy\",\n  \"body\": \"Both `UserLocationOperation` and `ReverseGeocodeUserLocation` accept a `CLLocationAccuracy` argument which has a sensible default value of three kilometers.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Chain operations together with Result Injection\",\n  \"body\": \"All location based operations conform to `ResultOperationType` which means their results can be injected as the requirement of other operations. See [Injecting  Results](doc:injecting-results).\"\n}\n[/block]","excerpt":"Location Services wrapped up in Operations","slug":"location-operation","type":"basic","title":"Location Operation(s)"}

Location Operation(s)

Location Services wrapped up in Operations

Operations has support for location services on iOS. [block:api-header] { "type": "basic", "title": "User Location" } [/block] `UserLocationOperation` can retrieve the location of the user's device. Internally it adds an `AuthorizedFor` condition using `Capability.Location`. See the section on [Capabilities](doc:capabilities) for more detailed information, but for now, it's enough to know that the operation will handle authorization for location services automatically. The class can be used with a completion block as in the example below [block:code] { "codes": [ { "code": "let operation = UserLocationOperation { location in\n print(\"Location: \\(location)\")\n}\nqueue.addOperation(operation)", "language": "swift", "name": "user location with completion block" } ] } [/block] Alternatively, `UserLocationOperation` sets the resultant `CLLocation` as a property: `location`. [block:code] { "codes": [ { "code": "let operation = UserLocationOperation()\noperation.addObserver(FinishedObserver { op in\n if let op = op as? UserLocationOperation {\n print(\"Location: \\(op.location)\") \n }\n})\nqueue.addOperation(operation)", "language": "swift", "name": "accessing property of user location" } ] } [/block] The above example is little redundant. A more likely usage of the `location` property would be when the operation is used inside of a `GroupOperation` subclass. See [Groups](doc:groups) for more guidance on creating groups. [block:api-header] { "type": "basic", "title": "Reverse Geocode" } [/block] Core Location supports [geocoding](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/index.html#//apple_ref/occ/cl/CLGeocoder) functionality. Reverse geocoding is where a known location is looked up to retrieve human readable location information such as an address. The actual result type for this information is a `CLPlacemark`. In Operations, this can be used with a `ReverseGeocodeOperation`: [block:code] { "codes": [ { "code": "let operation = ReverseGeocodeOperation(location: aLocation) { placemark in\n print(\"Placemark: \\(placemark)\")\n}\nqueue.addOperation(operation)", "language": "swift", "name": "reverse geocode" } ] } [/block] It also stores the received placemark in a property similar to `UserLocationOperation`. [block:api-header] { "type": "basic", "title": "Reverse Geocode User Location" } [/block] Putting this all together, we have `ReverseGeocodeUserLocation` which first get the device's current location and then use that for a reverse geocode lookup. Therefore, if you functionality in your app where the user's can enter their current address, it might make sense to use this operation. The returned `CLPlacemark` can return a localized address information. [block:code] { "codes": [ { "code": "let operation = ReverseGeocodeUserLocation { location, placemark in\n print(\"Placemark: \\(placemark)\")\n}\nqueue.addOperation(operation)", "language": "swift", "name": "reverse geocode user location" } ] } [/block] [block:callout] { "type": "info", "title": "Accuracy", "body": "Both `UserLocationOperation` and `ReverseGeocodeUserLocation` accept a `CLLocationAccuracy` argument which has a sensible default value of three kilometers." } [/block] [block:callout] { "type": "success", "title": "Chain operations together with Result Injection", "body": "All location based operations conform to `ResultOperationType` which means their results can be injected as the requirement of other operations. See [Injecting Results](doc:injecting-results)." } [/block]