I have this simple piece of code that of course correctly ran in Swift 5:
func geoRegion()-> CLRegion?{
guard let location=referenceLocation else{
return nil
}
return CLCircularRegion(center:location.coordinate, radius:50000, identifier:"georeferencing")
}
func placemarksForAddress(_ address: String) async throws -> [CLPlacemark]?{
if let placemark=placemarkCache[address]{
if placemark.location!.distance(from: referenceLocation!)<100000{
return [placemark]
}
}
do{
guard let geoRegion=self.geoRegion() else {
return nil
}
let placemarks = try await georeferenceQueue.geocodeAddressString( address, in: geoRegion)
if placemarks.count>=0{
self.placemarkCache[address]=MKPlacemark(placemark: placemarks[0])
return placemarks
}
} catch {
let placemarks=try await self.placemarkForLocation(referenceLocation)
return placemarks
}
return nil
}
That now presents error:
Sending task-isolated 'geoRegion' to actor-isolated instance method 'geocodeAddressString(_:in:)' risks causing data races between actor-isolated and task-isolated uses
fbartolom wrote:
As a matter of fact I moved everything in the actor and the error went away.
Cool. I’m glad you got this resolved.
ps It’s better to reply as a reply, rather than in the comments; see Quinn’s Top Ten DevForums Tips for this and other titbits.
none of the classes defined in any Apple API are actually sendable
Hmmmm, that’s a bit extreme. There are plenty of classes that are sendable in our various platform SDKs.
For the ones that aren’t, there are two common reasons:
- The class really isn’t sendable.
- The class is probably sendable but hasn’t been annotated as such.
Sadly, it’s hard to distinguish these cases. Which makes things tricky because the available paths forward vary by case:
- If the class really isn’t sendable, your only good option is, as you say, extract the data from the class into your own type that is sendable, send that, and then, if necessary, reconstruct the class instance on the other side.
- In the other case, you also have the option of doing something unsafe.
Honestly, I tend to favour the first option is almost all situations.
Looking at CLRegion, on first glance it looks like it should be sendable. However, this is Objective-C, and thus subclasses are a concern. And the CLBeaconRegion looks a lot less sendable than its superclass (note those non-atomic properties!)
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"