Wednesday, 9 January 2019

NativeScript Schematics - singleton Service constructor getting called multiple times

Singleton Service constructor getting called multiple times in a nativescript-schematics code sharing project. Here is some important dependencies version from my package.json:

"tns-android": {"version": "5.0.0"} "@angular/core": "~7.1.0", "nativescript-angular": "^7.1.0", "tns-core-modules": "^5.0.5", "@nativescript/schematics": "^0.4.0", "nativescript-dev-typescript": "^0.7.8", "nativescript-dev-webpack": "^0.17.0", "typescript": "~3.1.1"

I have tried providedIn: 'root' described in angular official docs and checking the singletonInstance as well. Constructor is getting called multiple times.

@Injectable({  providedIn: 'root'})
export class UserService {
  constructor(private _http: HttpClient) {
if (!UserService.singletonInstance) {
  console.log('in user service constructor');
  UserService.singletonInstance = this;
} else {
  return UserService.singletonInstance;
}

}

Do I need to use forRoot as we have app.module.ts and app.module.tns.ts ?



from NativeScript Schematics - singleton Service constructor getting called multiple times

1 comment:

  1. This blog really helpful for those who want to know about nativescript schemative singleton. If you are looking to Hire Nativescript Developers then you must aware about these things.

    ReplyDelete