How to automatically shift from Not Running state to BackGround state and keep BackGround state even without user's operation after the application is forced to close due to lack of memory or user's operation.

Purpose: How to automatically shift from Not Running state to BackGround state and keep BackGround state even without user's operation after the application is forced to close due to lack of memory or user's operation.

Current Situation: Even if the app is killed due to lack of memory or user interaction, and the app is in Not Running state, the API of enableBackgroundDelivery (https://developer.apple.com/documentation/healthkit/hkhealthstore/1614175-enablebackgrounddelivery) to detect healthcare data updates.

Even if the app is in Not Running status, it will temporarily transition from Not Running to BackGround status by detecting health care data updates using the enableBackgroundDelivery API ().

I would like to maintain the BackGround state by starting the location monitoring process when it temporarily becomes BackGround.

However, it is not possible to start the location monitoring process in the first place. Is it possible to start the location monitoring process after detecting the update of healthcare data and maintain the BackGround status?

Specifically, in the following code, the self.locationManager.start() process cannot be started.

 let locationManager: LocationManager = LocationManager()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     
    let bodyMass = HKSampleType.quantityType(forIdentifier: .bodyMass)!
    HKHealthStore().requestAuthorization(toShare: nil, read: Set([bodyMass, stepCount]), completion: { success, error in
    })     

    let query = HKObserverQuery(sampleType: bodyMass, predicate: nil, updateHandler: {
      query, completionHandler, error in
      if error != nil {
        return
      }

      // I'd like to get start CLLocationManager
      self.locationManager.start()

      completionHandler()
    })
     
    HKHealthStore().execute(query)

    HKHealthStore().enableBackgroundDelivery(for: bodyMass, frequency: .immediate, withCompletion: { success, error in
    })

    return true
  }

--

Postscript: The details of LocationManager is as follows

class LocationManager: NSObject {
   
  private let manager = CLLocationManager()

  override init() {
    super.init()
    manager.delegate = self
    manager.requestAlwaysAuthorization()
    manager.showsBackgroundLocationIndicator = true
    manager.desiredAccuracy = kCLLocationAccuracyBest 
    manager.distanceFilter = kCLDistanceFilterNone
    manager.pausesLocationUpdatesAutomatically = false  
    manager.allowsBackgroundLocationUpdates = true
  }
   
  func start() {
    NSLog("NSLog: Start!")
    manager.startUpdatingLocation()
  }

  func stop() {
    manager.stopUpdatingLocation()
  }
}

How to automatically shift from Not Running state to BackGround state and keep BackGround state even without user's operation after the application is forced to close due to lack of memory or user's operation.
 
 
Q