cmTanko
cmTanko

Reputation: 115

Unable to access angular services from nativescript worker

I am following this tutorial for creating a background threading, https://docs.nativescript.org/core-concepts/multithreading-model#workers-api

This works for simple cases, but then I have app.module.ts

@NgModule({
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    imports: [
        NativeScriptModule,
        NativeScriptRouterModule,
        NativeScriptHttpModule,
        NativeScriptFormsModule,
        NativeScriptRouterModule.forRoot(appRoutes),
        LoginModule,
    ],
    providers: [
        HttpService,
        UserService,
        LogService,
        DatabaseService
    ],
    schemas: [NO_ERRORS_SCHEMA]
})

How can I use service like UserService inside w.onmessage() , without using dependency injection. Or how to use a injectable class with w.onmessage()

Upvotes: 2

Views: 377

Answers (1)

Eduardo Speroni
Eduardo Speroni

Reputation: 341

Unfortunately, you can't. NS workers run in a completely different context and there is no shared memory.

I'd recommend using workers mainly to process heavy data and send it back to the main thread. The only other options are dependency injection and duplicating code.

Upvotes: 1

Related Questions