I'm using NGRX with Nativescript Angular to store state and fetching data from server. However everytime if i suspend the app (IOS) when the app is fetching data from server, the whole process will stop and loses all its states. When i open and resume the app, no data is being returned. Is there any method that i can handle this?
Here are the process:
component.ts (Calling NGRX action to load data) -> actions.ts (Trigger Effect to load data) -> effects.ts (Call service to load data from server, this would be cancelled if the app is suspend and resume during this process)
component.ts
this.store.dispatch(new DataActions.LoadData(this.pageNumber));
action.ts
export class LoadData implements Action {
readonly type = ActionTypes.LoadData;
constructor(
public pageNumber: number) {
console.log('load more data action'); //This is seen in the log
}
}
effect.ts
@Effect()
loadData$: Observable<Action> = this.actions$.pipe(
ofType<DataActions.LoadData>(
DataActions.ActionTypes.LoadData
),
mergeMap((action: DataActions.LoadData) =>
this.dataService.getData(action.pageNumber).pipe(
map((data: any) => {
console.log(data.result); //This is not seen in the log when app is suspend
return new DataActions.LoadDataSuccess(data.result);
}),
catchError(err => of(new DataActions.LoadDataFailed(err)))
)
)
);
dataService.service.ts
getData(pageNumber: number): Observable<PaginatedResult<Data[]>> {
const paginatedResult: PaginatedResult<Data[]> = new PaginatedResult<Data[]>();
let queryString = '?';
if (pageNumber != null) {
queryString += 'pageNumber=' + pageNumber ;
}
return this.http
.get(this.baseUrl + queryString, {observe: 'response'})
.pipe(
map((response: any) => {
paginatedResult.result = response.body;
console.log(paginatedResult.result); //This is not seen in the log if app suspend
return paginatedResult;
})
);
}