Tasks and jobs

Tasks are bound to online or offline data or services and provide methods to perform asynchronous operations using those resources.

With tasks you can:

  • Download, collect, and update geographic information using GeodatabaseSyncTask
  • Download and display tiled basemaps using ExportTileCacheTask
  • Locate addresses on the map and interpolate addresses from map locations using LocatorTask
  • Calculate point-to-point or multi-stop routes and get driving directions using RouteTask
  • Perform complex GIS analysis by executing geoprocessing models using GeoprocessingTask

Tasks either return their results directly from asynchronous methods on the task, or make use of jobs to provide status updates and results.

Tasks

Some operations return results directly from asynchronous methods on the task. For more complex or longer running operations, tasks make use of jobs instead.

To use tasks that return results directly:

  1. Create the task by initializing it to use the required data or service.
  2. Define the task inputs.
    • Some operations require only simple value inputs (for example a simple geocode operation may only require an address string as input).
    • Others require input parameters (for example, to limit a geocode operation to a specific country).
  3. Call the async operation method, passing in the inputs you defined.
  4. Use the results from the operation as required, for example to display geocode results on a map.

The code below creates a LocatorTask using the Esri geocode service, and passes in an address to geocode. When the operation is complete, the result location is retrieved and displayed in a GraphicsOverlay.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Call the geocode asynchronous method passing in an address.
final LocatorTask onlineLocator =
        new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");

final ListenableFuture<List<GeocodeResult>> geocodeFuture =
        onlineLocator.geocodeAsync("380 New York Street, Redlands, CA");
geocodeFuture.addDoneListener( () -> {
    try {
        // Get the results of the asynchronous operation.
        List<GeocodeResult> geocodeResults = geocodeFuture.get();

        if (!geocodeResults.isEmpty()) {
            // Use the first result - for example display in an existing graphics overlay.
            GeocodeResult topResult = geocodeResults.getFirst();

            Graphic gecodedLocation =
                    new Graphic(topResult.getDisplayLocation(), topResult.getAttributes(),
                            new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, Color.RED, 20.0f));

            mGraphicsOverlay.getGraphics().add(gecodedLocation);
        }
    } catch (InterruptedException | ExecutionException e) {
        // Deal with exception appropriately...
        dealWithException(e);
    }
});

Define input parameters

Tasks offer numerous options that allow you to tailor the operation to your requirements. For example, when geocoding you can restrict your search to a specific area, country, category of place, and/or number of results. When an author publishes a service or packages a resource, they can choose default values for these options that suit the specific data or the most common use case for the service.

To use these default parameter values, tasks provide helper methods that create parameter objects initialized with service-specific values. You can then make any changes to the parameter values before passing them to an operation. Creating these default parameter objects is useful for operations with many options, such as tracing a utility network.

The code below gets the default parameters for a RouteTask, and then ensures that results using these parameters will return both a route and directions, and also that the output spatial reference matches that of the map view.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Use asynchronous method on the route task class to get the default parameters
// (task can be loaded or not loaded).
final ListenableFuture<RouteParameters> defaultParametersFuture =
        routeTask.createDefaultParametersAsync();
defaultParametersFuture.addDoneListener(() -> {
    try {
        // Get the parameters from the future.
        RouteParameters routeParameters = defaultParametersFuture.get();

        // Update properties of the route parameters.
        routeParameters.setReturnDirections(true);
        routeParameters.setReturnRoutes(true);

        if (routeParameters.getOutputSpatialReference() != mapView.getSpatialReference()) {
            routeParameters.setOutputSpatialReference(mapView.getSpatialReference());
        }

        // Use the updated parameters to solve a route...
    } catch (InterruptedException | ExecutionException e) {
        // Deal with exception appropriately...
        dealWithException(e);
    }
});

Some parameters objects have constructors that you can use if you know the values of all the input parameters you want to use. This can be more efficient when parameter settings are simple.

For example, the code below creates geocoding parameters that restrict the country within which to geocode, and to limit the maximum returned results.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
GeocodeParameters geocodeParams = new GeocodeParameters();
geocodeParams.setCountryCode("France");
geocodeParams.setMaxResults(5);

Work online or offline

Many tasks can work either online by using services, or offline by using local data and resources. For example, you can geocode an address by using the default Esri geocoding service, your own geocoding service, a locator file (.loz), or a mobile map package (.mmpk).

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Create an online locator from a geocoding service.
// This code uses the Esri world geocode service.
final LocatorTask onlineLocator =
        new LocatorTask("https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer");

// Create an offline locator from a local .loc file.
// Coverage will depend on the packaged locator dataset.
final LocatorTask offlineLocator = new LocatorTask(localLocatorPath);

// Mobile map packages can also contain locators.
// Use the following code to get a reference to an offline locator.
final MobileMapPackage mmpk = new MobileMapPackage(localMmpkPath);
mmpk.loadAsync();
mmpk.addDoneLoadingListener(() -> {
    if (mmpk.getLoadStatus() == LoadStatus.LOADED) {
        LocatorTask mmpkLocator = mmpk.getLocatorTask();

        // Use locator from a mobile map package according to the packaged locator coverage.
    }
});

Tasks and jobs

Some tasks expose operations that have multiple stages (like preparing and downloading a geodatabase), and can generate multiple progress messages (such as percentage complete). These types of tasks are always bound to ArcGIS Server (or Local Server for platforms that support it). An example is GeodatabaseSyncTask.generateGeodatabase().

Instead of returning results directly, these tasks make use of jobs to monitor status, return progress updates, and return their results. Each Job represents a specific operation of a task. Jobs are useful for longer-running operations, because they can also be paused, resumed, and canceled. Your app can support a user action or host OS terminating a running job object, and then recreate and resume the job later.

To use operations like these:

  1. Create the task by initializing it to use the required data or service.
  2. Define the input parameters for the task.
  3. Call the async operation method to get a job, passing in the input parameters you defined.
  4. Start the job.
  5. Optionally, listen for changes to the job status and check the job messages, for example to update a UI and report progress to the user.
  6. Listen for the job completion and get the results from the operation. Check for errors in the job, and if successful, use the results.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Create the export tile cache task.
ExportTileCacheTask exportTilesTask = new ExportTileCacheTask(tiledLayer.getUri());

// Create parameters for the export tiles job.
double mapScale = mapView.getMapScale();

// The max scale parameter is set to 10% of the map's scale to limit the
// number of tiles exported to within the tiled layer's max tile export limits.
ListenableFuture<ExportTileCacheParameters> exportTileParametersFuture =
        exportTilesTask.createDefaultExportTileCacheParametersAsync(downloadArea, mapScale, mapScale * 0.1);
// Wait for the export tile parameters to finish.
exportTileParametersFuture.addDoneListener(() -> {
    try {
        // Create a file.
        File tempFile = File.createTempFile("tiles", ".tpk");
        tempFile.deleteOnExit();

        String exportedTpkPath = tempFile.getAbsolutePath();

        // Get the export tile parameters.
        ExportTileCacheParameters exportTilesParameters = exportTileParametersFuture.get();

        ExportTileCacheJob exportJob = exportTilesTask.exportTileCache(exportTilesParameters, exportedTpkPath);

        // Start the job.
        exportJob.start();

        // Listen for changes to the job status and check the job messages.
        // Listen for job completion.

    } catch (InterruptedException | ExecutionException | IOException e) {
        new Alert(Alert.AlertType.ERROR, e.getMessage()).show();
    }
});

Calling Job.getStatus() retrieves the current Job.Status in the job's workflow. Jobs periodically fire a changed event as they are running, usually with decreasingly frequency as a job progresses. More than one Job.Message may appear in a change event. The job complete listener is called as soon as the job finishes. Whether successful or not, jobs cannot be restarted.

Report job progress

A job represents an asynchronously running operation that might take some time to finish. As described previously, you can monitor changes to job status for notification when a job has completed, failed, or been canceled, but what about the time in-between? Users may become frustrated waiting for a long job to complete without getting feedback on its progress. Fortunately, jobs provide a mechanism for reporting the current progress (percentage complete) for the running operation they represent.

As the job runs, the status changed listener, the job message added listener, or both, are called. (Listen using the Job.addStatusChangedListener() and Job.addJobMessageAddedListener(), respectively). You can get the current progress of the job at any point from the job's Job.getProgress() property, an integer representing the percentage of the operation that has been completed. This allows your app to provide more specific information about the status of a running job using UI elements like progress bars, for example.

The following example updates the UI element with the percentage complete for the job and then gets the Tile Cache when complete.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Update the progress bar with the job's progress.
exportJob.addProgressChangedListener(() -> progressBar.setProgress(exportJob.getProgress() / 100.0));

// Listen for job status updates, and report the most recent message to the user.
exportJob.addStatusChangedListener(() -> {
    List<Job.Message> messages = exportJob.getMessages();

    updateUiWithProgress(messages.getLast().getMessage());
});

// Listen for when the job is completed.
exportJob.addJobDoneListener(() -> {
    if (exportJob.getStatus() == Job.Status.SUCCEEDED) {
        // Get the tile cache resulting from the successful export job,
        // and use it by adding a layer to a map in the map view.
        final TileCache exportedTileCache = exportJob.getResult();

        ArcGISTiledLayer tiledLayerPreview = new ArcGISTiledLayer(exportedTileCache);

        map.setBasemap(new Basemap(tiledLayerPreview));

        mapView.setMap(map);
    }

    if (exportJob.getError() != null) {
        // Deal with export job error appropriately...
        dealWithException(exportJob.getError());
    }
});

Pause, resume, or cancel a job

Jobs are designed to handle a user exiting an app while the job is running or having the app terminated by the host operating system. Jobs also provide a mechanism for explicitly pausing or canceling the operation.

Cancel a job

Sometimes, the results of a job are no longer required. For example, a user could change their mind about the area of a tile cache they want to download and want to cancel the job and start over.

Calling Job.cancelAsync() changes Job.Status to canceling, cancels the Job, and waits for any asynchronous, server-side operations to be canceled. After all cancelation tasks complete (including any server-side tasks), Job.Status changes to failed and Job.cancelAsync() returns true. If one or more jobs cannot be canceled, Job.cancelAsync() returns false.

For example, GenerateOfflineMapJob is a server-side job that launches several more server-side jobs, depending on the layers in your map. Other examples of server-side jobs include ExportTileCacheJob, ExportVectorTilesJob, GenerateGeodatabaseJob, and GeoprocessingJob.

You should always cancel unneeded jobs (for example when exiting your app) to avoid placing unnecessary load on the server.

The code below shows, for a running ExportTileCacheTask, adding a Job.addJobDoneListener(). Within the listener the code checks for the appropriate error code and domain that indicates that the job has been canceled. At this point, the code checks in case the tile cache file was already created, and if so, deletes it.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// For a job that is still running, listen for when the job is completed.
inProgressJob.addJobDoneListener(() -> {
    // Check if there was an error.
    ArcGISRuntimeException jobError = inProgressJob.getError();

    if (jobError != null) {
        // Failure status can indicate cancellation, or other types of failure.
        if (inProgressJob.getStatus() == Job.Status.FAILED) {
            // Check the error information to confirm if this was a user cancellation.
            if ((jobError.getErrorCode() == 18) &&
                (jobError.getErrorDomain() == ArcGISRuntimeException.ErrorDomain.ARCGIS_RUNTIME)) {

                // UI can be updated to indicate cancellation.
                System.out.println("Export has been cancelled");

                // Could check if tile cache was downloaded before the task was cancelled
                // and clean up the file.
                File tileCacheFile = new File("local/path/to/store/file/.tpk");

                if (tileCacheFile.exists()) {
                    tileCacheFile.delete();
                }
            }
        }
        // Deal with other types of failures...
    }
    // Deal with a successful job...
});

Pause and resume a job

Jobs can be long-running operations, so there is no guarantee that they will be completed while the app is running. You can pause a job explicitly using Job.getProgress(). For example, when an app is backgrounded and does not have permissions for background operation. Pausing may also be useful if a user wishes to temporarily stop network access for any reason.

Job changed messages will not be received for a paused job. Pausing a job does not stop any server-side processes from executing. While a job is paused, outstanding requests can complete. Therefore, when resuming a job it may have a different state than when it was paused.

You can serialize a job to JSON to persist it if your app is backgrounded or the process is otherwise terminated. When you deserialize it again the Job.Status will be in the paused state regardless of its state when serialized and should be restarted to resume listening for completion. The job changed listener is a good place to update the job JSON for storage by your app.

The code below shows, for an existing running Job, serializing the job to JSON.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
inProgressJob.addStatusChangedListener(() -> {
    // Every time the job changes, update the stored JSON that represents the job.
    storedJobJson = inProgressJob.toJson();
});

The Job can then be deserialized back from stored JSON, and restarted. Remember to set the job changed and done listeners again to be informed when the job changes, and when it is complete and the result can be retrieved.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
if (storedJobJson != null && !storedJobJson.isEmpty()) {
    inProgressJob = (ExportTileCacheJob) Job.fromJson(storedJobJson);

    if (inProgressJob != null) {
        // Deserialized jobs have a job status of paused, so restart the job.
        inProgressJob.start();

        // Resume listening for status changes and job completion.
        inProgressJob.addStatusChangedListener(() -> {
            // Deal with job changes.
        });

        inProgressJob.addJobDoneListener(() -> {
            // Deal with job done.
        });
    }
}

Loss of network connection

Additionally, jobs using services are designed to handle situations where network connectivity is temporarily lost without needing to be immediately paused. A started job will ignore errors such as network errors for a period of up to 10 minutes. If errors continue for longer, the job will fail and the message will indicate the loss of network connection.

To handle inconsistent connectivity, you can serialize and pause a job when your app loses connectivity for a few minutes to avoid job failure (as failed jobs cannot be restarted). The job can then be deserialized and resumed when connectivity returns.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.