ngrx store does not update UI after dispatched action from firebase promise

落爺英雄遲暮 提交于 2020-01-24 20:16:18

问题


I am trying to develop a basic NativeScript app using Angular 4. I am also using ngrx store and firebase as a realtime database.

When I use a sample array and update the store and not integrate with firebase the program works fine. UI gets updated too.

let value = [
    {
        "description": "Groups discussing about Technology",
        "category": "Tech"
    },
    {
        "description": "Groups for all kinds of sports ",
        "category": "Sports"
    }
];

this.store.dispatch({ type: GroupCategoryListActions.ADD_ITEMS, payload: value });
this.store.dispatch({ type: GroupCategoryListActions.LOAD_SUCCESS });

However when I integrate with firebase and try to retrieve the same data using a firebase query, although the query returns the same array as in the above code sample but the UI doesn't get updated. Below is the code to fetch from firebase.

firebase.init()
    .then((result) => {
        firebase.query(
            (result) => {
                if(!result)
                    this.store.dispatch({ type: GroupCategoryListActions.LOAD_ERROR });

                this.store.dispatch({ type: GroupCategoryListActions.ADD_ITEMS, payload: result.value });
                this.store.dispatch({ type: GroupCategoryListActions.LOAD_SUCCESS });
            },
            '/groupCategory',
            {
                orderBy: {
                    type: firebase.QueryOrderByType.KEY,
                    value: 'category'
                }
            });
    },
        (error) => console.log("firebase.init error: " + error)
    );

Here is the url to my firebase database


回答1:


That's entirely normal behavior for Angular; the query promise is not part of the Angular lifecycle. That's not specific to the Firebase plugin by the way.

You can use NgZone to make the result attach to the Angular lifecycle so your view will update. Inject zone: NgZone in your @Component constructor and wrap the stuff after (result) => { in this.zone.run(() => { /* your result-handling code here */}).



来源:https://stackoverflow.com/questions/45651990/ngrx-store-does-not-update-ui-after-dispatched-action-from-firebase-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!