CLMonitor init function causes app crash

I'm experiencing app crashes when calling the CLMonitor initialization function:

let monitor = await CLMonitor("my_monitor")

According to WWDC 2023: Meet Core Location Monitor, when creating a CLMonitor object with the same identifier, it should access the existing monitor without any mention of app crashes or buggy behavior.

However, in my actual testing, attempting to create a CLMonitor object with the same identifier immediately causes an app crash.

Here's part of the crash log:

Last Exception Backtrace:
0   CoreFoundation                	0x19c4ab21c __exceptionPreprocess + 164 (NSException.m:249)
1   libobjc.A.dylib               	0x199945abc objc_exception_throw + 88 (objc-exception.mm:356)
2   Foundation                    	0x19b7a9670 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 288 (NSException.m:252)
3   CoreLocation                  	0x1aa25cbb4 +[CLMonitor _requestMonitorWithConfiguration:locationManager:completion:] + 516 (CLMonitor.mm:516)
4   libswiftCoreLocation.dylib    	0x22bf6085c CLMonitor.init(_:) + 488 (CLMonitor.swift:280)
5   libswiftCoreLocation.dylib    	0x22bf604b9 <deduplicated_symbol> + 1
6   MiniPlengi                    	0x106372ec9 closure #1 in static CLMonitor.loplatMonitor.getter + 1 (CLMonitor+Extensions.swift:31)
7   MiniPlengi                    	0x1062ce325 0x106290000 + 254757
8   MiniPlengi                    	0x1062f6a29 specialized thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1 (/<compiler-generated>:0)
9   MiniPlengi                    	0x1062ce325 0x106290000 + 254757
10  libswift_Concurrency.dylib    	0x1a7f75241 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1 (Task.cpp:537)

Furthermore, even though I've written code to create CLMonitor objects based on a singleton structure to handle these crash cases, the app still crashes:

extension CLMonitor {
    static var loplatMonitor: CLMonitor {
        get async {
            struct Static {
                static var monitor: CLMonitor?
                static var initializationTask: Task<CLMonitor, Never>?
            }

            // If already initialized
            if let monitor = Static.monitor {
                return monitor
            }

            // If there's an initialization task in progress, wait for its result
            if let task = Static.initializationTask {
                return await task.value
            }

            // Create new initialization task
            let task = Task {
                let monitor = await CLMonitor("my_monitor")
                Static.monitor = monitor
                Static.initializationTask = nil  // Clean up task after completion
                return monitor
            }

            Static.initializationTask = task
            return await task.value
        }
    }
}

Is the CLMonitor API still in a stabilization phase and not recommended for production release? I would appreciate guidance on the correct usage.

If these issues are expected to persist, I'm wondering if I should continue using the existing CLCircularRegion API instead.

Any insights or recommendations would be greatly appreciated.

The CLMonitor API is certainly production ready, and as documented, when the app relaunches, you are expected to recreate the monitor with the same identifier.

The issue is possibly being triggered by something else in your code.

My suggestion would be to create a simple app that only monitors your location, or use the sample app Monitoring location changes with Core Location to see for yourself that this works as documented, and then debug your own code to find what might be causing this crash.


Argun Tekant /  WWDR Engineering / Core Technologies

CLMonitor init function causes app crash
 
 
Q