Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facing Issue with using Core Data context. #21

Open
amberkatyal opened this issue Sep 21, 2023 · 0 comments
Open

Facing Issue with using Core Data context. #21

amberkatyal opened this issue Sep 21, 2023 · 0 comments

Comments

@amberkatyal
Copy link

amberkatyal commented Sep 21, 2023

AppDelegate:

import Foundation
import RouterService
import RouterServiceInterface
import CoreData

extension AppDelegate {
    
    func registerDependencies() {
        routerService.register(dependencyFactory: {
            AppDelegate.shared.persistentContainer.viewContext
        }, forType: NSManagedObjectContext.self)
    }
}
import RouterService
import RouterServiceInterface

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    static let shared = UIApplication.shared.delegate as? AppDelegate ?? AppDelegate()
    
    private(set) var persistentContainer = myContainer
    
    let routerService = RouterService()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        createPersistentStoreContainer { container in
            self.persistentContainer = container
        }
        ...
        // Register Dependencies
        registerDependencies()
        
        return true
    }

Usage:

import CoreData
import RouterServiceInterface

final class ProfileDashboardViewModel {
    
    // MARK: - Properties
    let navigationLeftIconResource: ImageName
    let title: String
    let employeeID: String
    let employeeName: String
    
    lazy private(set) var employeeProfile: EmployeeProfile = {
        if let profile = EmployeeProfile.fetch(for: self.employeeID, in: self.managedObjectContext) {
            return profile
        }
        let employeeProfile = EmployeeProfile(context: managedObjectContext)
        employeeProfile.employeeID = self.employeeID
        employeeProfile.name = self.employeeName
        return employeeProfile
    }()
    
    @Dependency var managedObjectContext: NSManagedObjectContext

Here getting the following error:
Fatal error: Attempted to use Dependency without resolving it first!

Also when I put a breakpoint inside the register(dependencyFactory: {} closure it never hits the breakpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant