Dependency Injection init confusion

Noob here trying to use dependency injection and not entirely understanding all the parts.

I have a custom CLLocationManagerDelegate class that gets user location by lat/long if locationservices are authorized, and then shares it with multiple View Controllers. I’m now trying to save the lat/long to Core Data and I have created another custom PersistanceManager class to handle savingContext and fetching.

Now I’m trying to init the PersistanceManager class in my custom CLLocationManagerDelegate class, but running into some issues with init() that I dont understand.

Here’s I add final class PersistanceManager right now:

class CustomLocationManager: NSObject {
static let shared = CustomLocationManager()
let persistenceManager: PersistanceManager

private var locationManager = CLLocationManager()
weak var delegate: LocationServicesDelegate?

private override init() {
    self.persistenceManager = persistenceManager * ERROR

convenience init(persistenceManager: PersistanceManager) {
    self.init(persistenceManager: persistenceManager)

I get an error "Assigning property to itself " at the error.

I understand that convenience init is secondary and so I thought I should init the PersistanceManager first so it exists before the CustomLocationManager’s super.init() is called. So why am I getting this error?

@amritpan Do you still have issues with this?

No I figured out this specific issue by moving my code into a Coordinator pattern.

Couple questions I have just in looking at your code (since moving to a Coordinator pattern doesn’t automagically solve your dependency injection woes)…

  1. why is your custom class inheriting from NSObject?
  2. your convenience init is calling itself?
  3. you’re overriding NSObjects initializer and in that init, persistenceDataManager isn’t actually set which is why your error is popping up.

This topic was automatically closed after 166 days. New replies are no longer allowed.