Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How can I force a dependency to be resolved on a background thread #332

@luoshuaiqing

Description

@luoshuaiqing

I want to force one of my dependency to be resolved on background thread. Because it is a @ModelActor, which thread it is created will determine which thread it will execute the DB operations. Since I want the DB operations to always execute on the BG thread, so I want to create the Model Actor on the background thread as well.

Here's my code:

import Foundation
import Factory

@ModelActor
actor DatabaseManager {
    @Injected(\.errorLogger) private var errorLogger

    private var modelContext: ModelContext {
        modelExecutor.modelContext
    }
}

extension Container {
    var databaseManager: Factory<DatabaseManager> {
        self {
            DatabaseManager(modelContainer: self.modelContainer.resolve())
        }.singleton
    }
}

Would appreciate your help, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions