0

I'm using locationManager(_:didVisit:) from Core Location to get notified when the user arrives at or leaves a location.

However, I find the callback might be called twice sometimes. Because I would save the data I receive into Core Data, I'd like to remove the duplications in those rare cases.

I have no experience with concurrency in Swift. How can I do to avoid the duplicated ClVisit entries.

    func locationManager(_ manager: CLLocationManager, didVisit visit: CLVisit) {
        if a visit is already in DB {
            return
        }

        // saves the data to DB
    }
Sheffield
  • 355
  • 4
  • 18

1 Answers1

-1

There is no "duplication" and there is no concurrency issue; nothing is being executed "in parallel". These calls are made on the main thread; they cannot overlap. You get an event on arrival and another event on departure, with a different distinct CLVisit object each time. If you are recording this information in CoreData, coping with that is up to you.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • What I mentioned is a corner case that happens rarely. It's also confirmed by others from the community: https://developer.apple.com/forums/thread/101377 I was trying to eliminate the duplicated visits. – Sheffield Oct 08 '22 at 16:47
  • 1
    I stand by what I said. Even if the same visit _does_ arrive twice, it is absolutely impossible that this should happen in a "parallel" or "concurrent" manner, because the method is called on the main thread. – matt Oct 08 '22 at 18:28