Search operations. to find and display reports of real-world objects, to collect geographic information and analyze data presented on a map, can be achieved using tasks. These tasks provide the foundation of the geospatial functionality that you can add to your application.
This API includes the following task types:
- Query—Gets features from map services that satisfy attribute or spatial queries
- Identify—Gets features from map services at a location
- Find task—Gets features from map services that contain a particular value in their attributes
- GeometryService task—Performs geometric operations such as buffering, projecting, and simplifying
- Geoprocessor task—Does complex GIS analysis by executing geoprocessing models published as geoprocessing services
- Locator—Matches locations to addresses and vice-versa
- Route task—Computes point-to-point or multi-point routes and driving directions
- Service area task—Calculates areas that can be serviced (reached) from a given location
- Closest facility task—Finds facilities that are closest to a given incident
Work with tasks
All tasks inherit from AGSTask. Working with tasks is not complicated once the pattern is understood. All tasks follow the same basic principals and pattern. Tasks are wrappers around operations supported by ArcGIS Server REST web services. Tasks take care of asynchronously sending requests, showing network activity indicators, applying appropriate credentials to network resources, and parsing results.
To execute a task, work through the following pattern:
- Set delegate
- Retain task
- Kickoff operation
- Wait for delegate to fire
Since tasks inherit from AGSTask, they have the following initializers: initWithURL and initWithURL:credential. Since tasks rely on operations for ArcGIS Server REST web services, instantiate them with a URL. Some services are secured and require credentials.
Tasks work asynchronously; therefore, the calling object wants notification when the task succeeds or fails. This is where the delegate becomes important. The delegate is an object assigned to the task that implements a set of well-known methods. Typically, each task has a success and failure method. For example, AGSQueryTaskDelegate has the following methods:
- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation*)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet;
- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation*)op didFailWithError:(NSError *)error;
Notice, for the query operation there are two methods (one success and one failure). Choose whether to implement these methods if they are important for the application.
Retaining the task is important. Since tasks execute asynchronously, and kicking off the operation is a non-blocking call, retain your task. Creating a strong property for your task is one efficient and effective way to do this. The reason is that if the task is deallocated before the operation completes, it will not call back the delegate unless the task is retained.
To kick off an operation on the task, call the method of the operation you want to execute. For example, the AGSQuery task has the following method:
-(NSOperation *)executeWithQuery:(AGSQuery *)query;
Tasks return the operation they start, so that you can cancel it anytime. You can start as many operations as necessary, even on a single task. For example, you can have one query task that executes five queries simultaneously.
Wait for delegate to fire
After kicking off an operation, wait for the delegate to be notified of its success or failure. The task takes care of showing the network activity indicator for you while the operation executes. If an operation fails, it could be that the proper credentials were not supplied, the inputs were not valid, and so on. Check the returned NSError object to determine the reason for the error.