@@ -142,6 +142,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
142
142
}
143
143
self . navigationItem. leftBarButtonItems = [ folderButton]
144
144
145
+ _ = GPXFileManager . GPXFilesFolderURL. startAccessingSecurityScopedResource ( )
145
146
// Initial load of the data
146
147
let list : [ GPXFileInfo ] = GPXFileManager . fileList
147
148
gpxFilesFound = list. count != 0
@@ -155,6 +156,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
155
156
156
157
/// Removes notfication observers
157
158
deinit {
159
+ GPXFileManager . GPXFilesFolderURL. stopAccessingSecurityScopedResource ( )
158
160
NotificationCenter . default. removeObserver ( self )
159
161
}
160
162
@@ -313,34 +315,53 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
313
315
314
316
/// Given a GPX file loads the file in the
315
317
func loadGPXFile( gpxFileURL: URL ) {
318
+
319
+ self . displayLoadingFileAlert ( true )
316
320
DispatchQueue . global ( qos: . userInitiated) . async {
317
- DispatchQueue . main. sync {
318
- self . displayLoadingFileAlert ( true )
319
- }
320
- print ( " Load gpx File: \( gpxFileURL) " )
321
- guard let gpx = GPXParser ( withURL: gpxFileURL) ? . parsedData ( ) else {
322
- print ( " GPXFileTableViewController:: load of GPX file failed " )
323
- self . displayLoadingFileAlert ( false )
324
- return
325
- }
326
-
327
- DispatchQueue . main. sync {
328
- self . displayLoadingFileAlert ( false ) {
329
- self . delegate? . didLoadGPXFileWithName ( gpxFileURL. deletingPathExtension ( ) . lastPathComponent, gpxRoot: gpx)
321
+ print ( " GPXFIlesTableViewController:: loadGPXFile Load gpx File: \( gpxFileURL) " )
322
+ if GPXFileManager . GPXFilesFolderURL. startAccessingSecurityScopedResource ( ) {
323
+ defer {
324
+ GPXFileManager . GPXFilesFolderURL. stopAccessingSecurityScopedResource ( )
325
+ }
326
+ do {
327
+ DispatchQueue . main. sync {
328
+ self . displayLoadingFileAlert ( true )
329
+ }
330
+ guard let gpx = GPXParser ( withURL: gpxFileURL) ? . parsedData ( ) else {
331
+ print ( " GPXFileTableViewController:: load of GPX file failed " )
332
+ DispatchQueue . main. sync {
333
+ Toast . error ( " Could not open file " )
334
+ self . displayLoadingFileAlert ( false )
335
+ }
336
+ return
337
+ }
338
+ DispatchQueue . main. sync {
339
+ self . displayLoadingFileAlert ( false ) {
340
+ self . delegate? . didLoadGPXFileWithName ( gpxFileURL. deletingPathExtension ( ) . lastPathComponent, gpxRoot: gpx)
341
+ self . dismiss ( animated: true , completion: nil )
342
+ self . presentingViewController? . dismiss ( animated: true , completion: nil )
343
+ }
344
+ }
345
+ }
346
+ } else {
347
+ print ( " Could not access folder: \( GPXFileManager . GPXFilesFolderURL) " )
348
+ DispatchQueue . main. sync {
349
+ Toast . error ( " Could not open file " )
350
+ self . displayLoadingFileAlert ( false )
330
351
self . dismiss ( animated: true , completion: nil )
331
352
}
332
353
}
333
354
}
334
-
335
355
}
336
-
356
+
337
357
/// Loads the GPX file that corresponds to rowIndex in fileList in the map.
338
358
internal func actionLoadFileAtIndex( _ rowIndex: Int ) {
339
359
guard let gpxFileInfo: GPXFileInfo = ( self . fileList. object ( at: rowIndex) as? GPXFileInfo ) else {
340
360
print ( " GPXFileTableViewController:: actionLoadFileAtIndex( \( rowIndex) ): failed to get fileURL " )
341
361
return
342
362
}
343
363
print ( " GPXFileTableViewController:: Load gpx File: \( gpxFileInfo. fileName) " )
364
+ print ( " GPXFileTableViewController:: Load gpx URL: \( gpxFileInfo. fileURL) " )
344
365
loadGPXFile ( gpxFileURL: gpxFileInfo. fileURL)
345
366
}
346
367
@@ -365,7 +386,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
365
386
self . present ( alertController, animated: true , completion: nil )
366
387
} else { // will dismiss alert
367
388
activityIndicatorView. stopAnimating ( )
368
- self . presentingViewController? . dismiss ( animated: true , completion: nil )
389
+ // self.presentingViewController?.dismiss(animated: true, completion: nil)
369
390
}
370
391
371
392
// if completion handler is used
@@ -450,7 +471,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
450
471
/// For reloading the tableView in `GPXFileTableViewController`
451
472
///
452
473
/// It gets from the `GPXFileManager` the`fileList`
453
- ///
474
+ ///
454
475
///
455
476
@objc func reloadTableData( ) {
456
477
print ( " TableViewController: reloadTableData " )
0 commit comments