3

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 ?

Narendra
  • 4,514
  • 2
  • 21
  • 38
  • That should be enough. Can you share you module declaration? The service should NOT be in the "providers" sections, as it was provided in root. – Eduardo Speroni Dec 17 '18 at 02:33
  • yup, it is not there. providers: [],is empty in both app.module.ts and app.module.tns.ts – Narendra Dec 17 '18 at 02:37
  • That's weird, whe're not using schematics, but that shouldn't matter, since it gets replaced according to the platform. Does this happen on web or mobile? Is the service in the providers of any other module? – Eduardo Speroni Dec 17 '18 at 12:13
  • @Bass This is happenning on Web amd service is not there in the providers. – Narendra Dec 17 '18 at 22:50

2 Answers2

2
@Injectable({  providedIn: 'root'})
export class UserService {
  constructor(private _http: HttpClient) {
  }
}

Above code is enough. And you need import UserService in the controller, like

@import UserService from ...;
class MyController{
  constructor(private userService: UserService){}
}
Nick Wang
  • 624
  • 3
  • 6
  • Problem is When I use in another controller, class MyController2{ constructor(private userService: UserService){} }, UserService constructor i sgetting called again and I am unable to share variable between components. – Narendra Jan 09 '19 at 02:46
  • I know, please remove your source in constructor method first and try again. The constructor method should be not return anything, otherwise, the instance cannot be properly constructed. – Nick Wang Jan 09 '19 at 02:56
  • commented out everything from constructor, still the same. I created a variable in UserService public testForSO = 'FIRST';try to change in controller1 constructor {this._userService.testForSO = 'CHANGED_IN_HOME';} but when I print that in second conroller, it prints `FIRST` – Narendra Jan 09 '19 at 03:03
  • I think it is other question. for simple types the real value will copy, but for Object the pointer will be copy. May you can try define one object there, like `UserService.MyData = {testForSO: 'First'};`. But, best way is use subject & subscrbier. – Nick Wang Jan 09 '19 at 03:10
0

After doing some further investigation I found that Visual code intellisense has imported the service from .js file instead of .ts file. Hence it was making the multiple constructor calls. I am adding that as it may be helpful for someone’s debugging.

Narendra
  • 4,514
  • 2
  • 21
  • 38