nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

Change detection issue in ListView

Open ignaciofuentes opened this issue 10 years ago • 31 comments

Conditional styling in ListView templates don't get consistently applied. Most times they require the item to go off screen and back in, in order for the new status to be reflected.

Gif repro

Here is a sample to reproduce:

Component:

import {Component} from "@angular/core";
@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent {
    public options: string[] = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];

    public selectedOption: string = "";

    public onItemTap(args) {
        this.selectedOption = this.options[args.index];
    }
}

Markup:

<StackLayout>
<ListView #listview [items]="options" separatorColor="white" (itemTap)="onItemTap($event)">
    <template let-item="item" let-i="index" let-odd="odd" let-even="even">
        <GridLayout columns="44, *" height="48">
            <StackLayout colSpan="2" verticalAlignment="bottom" class="line"></StackLayout>      
            <Label col="1" [text]="item" style="font-size:20" [class.active]="selectedOption == item"></Label>
        </GridLayout>
    </template>
</ListView>
</StackLayout>

CSS

.active {
    color:red;
}

ignaciofuentes avatar Jul 28 '16 15:07 ignaciofuentes

It looks like a change detection issue. The list-view has OnPush change detection strategy (for performance reasons). Probably, thats why the items in it are note updated when selectedOption is changed.

One thing I noticed that if you use tap listener on the grid inside the template - it works as expected:

<GridLayout height="48" (tap)="selectedOption = item">

In this case, because the event is executed in the actual item - it triggers its change detector.

vakrilov avatar Aug 02 '16 10:08 vakrilov

I ran into a very similar issue, but adding a (tap) wasn't possible in my case.

However, I did solve it by changing the instance variable type to BehaviorSubject, and using the async pipe in the template.

jzgoda avatar Dec 29 '16 20:12 jzgoda

I am having the same issue, having a class toggled by an item's property (in a list-view). Is there a way to manually trigger change detector for an item? In my case the property is changed after a request to an API, so I can't do the change in the layout.

eduardoturconi avatar Jan 24 '17 03:01 eduardoturconi

Even though your problem is a little bit strange, you could try this:

import { Component, NgZone } from "@angular/core";

@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent {
    public options: string[] = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];

    constructor(private zone: NgZone) { }

    public selectedOption: string = "";

    public onItemTap(args) {
        this.zone.run(() => {
               this.selectedOption = this.options[args.index];
        });
    }
}

Once I had a problem like that and I fixed it by running the code inside the NgZone.

eleddie avatar Jan 24 '17 04:01 eleddie

Trying with NgZone didn't work for me. With RadListView, changing an item's property updates immediately the UI, but since it has a memory leak (an issue was created on telerik/nativescript-ui-feedback) and kills my app after a while, I can't use it. With ListView, the UI is not updated. Is there any alternative to NgZone that could work?

eduardoturconi avatar Jan 24 '17 14:01 eduardoturconi

After trying some modifications to my code, I could solve this issue by doing:

...
this.items[i].is_list = true;
this.items = this.items.slice();

That way, when reassigning to this.items it's own array, the UI got updated.

eduardoturconi avatar Jan 26 '17 00:01 eduardoturconi

Same problem. Thanks @eduardoturconi , that worked for me.

enissay14 avatar Mar 06 '17 16:03 enissay14

Worth noting: This isnt just on angular, I am experiencing this in a Typescript/vanilla app. I fix it by calling refresh() on the listview, though that shouldnt be necessary.

davecoffin avatar Sep 21 '17 14:09 davecoffin

This happens only on iOS, works correctly on Android.

erkanarslan avatar Dec 22 '17 20:12 erkanarslan

this is really a big problem on iOS, I've tried with NgZone.run, it did't work, also tried with refresh() also don't work at all.

but on android, I don't need NgZone nor refresh to make this kind of change work. it just work perfectly.

When could this be fixed?

steve3d avatar Jan 04 '18 17:01 steve3d

Also experience this issue on IOS...seems like it doing the buggy thing when bind function and works perfectly when bind to property

bzaruk avatar Jan 27 '18 14:01 bzaruk

Experiencing the same problem. The issue is specific to IOS for me as well.

Calling refresh() on the listview was the only suggestion to resolve the problem, though i'm hopeful that it is a temporary resolution to the bigger problem. Calling refresh() is causing the view to flash, which looks odd and makes for a poor UX. Hope this is still being looked into.

dillonstreator avatar Feb 14 '18 18:02 dillonstreator

Flashing problem as described by @DillonStreator can be "solved" by overwriting the default behaviour as seen here

<ListView [items]="items" (itemLoading)="onItemLoading($event)">
onItemLoading(args: ItemEventData) {
  const iosCell = args.ios;
  iosCell.selectionStyle = UITableViewCellSelectionStyle.None;
}

SomeDudeeee avatar Mar 21 '18 09:03 SomeDudeeee

Is there any possible option to fix this if the source collection doesn't change? IF i'm changing only property visibility="visible || collapsed" of the view of tapped ITEM . I've tried @eduardoturconi .slice() method and others like:

  • listView = this.page.getViewById("listView") listView.refresh();
  • BehaviourSubject and BehaviourSubject with NgZone.run
  • @Dagorwolf method; !!! Nothing work correctly Please, would appreciate any ideas.

ShyshkovOleg avatar Jul 17 '18 14:07 ShyshkovOleg

I solved this problem as described here.

Just add this:

    Object.defineProperty(View.prototype, "isLayoutValid", {
        get: function () {
            return (this._privateFlags & PFLAG_LAYOUT_REQUIRED) !== PFLAG_LAYOUT_REQUIRED;
        },
        enumerable: true,
        configurable: true
    });   

in

node_modules/tns-core-modules/ui/core/view/view.ios.js

And everything was magically fixed!

drmistral avatar Jul 19 '18 10:07 drmistral

Have the same issue, tried https://angular.io/api/core/ChangeDetectorRef but no success, tried ngZone and no success. Is it related to ListView? e.g If I change to a StackView with ScrollView around would it work?

onItemLoading() does not get triggered when coming backwards with router. Only forwards

artdias90 avatar Jul 19 '18 21:07 artdias90

@drmistral , if Im using angular and I would change view.ios.js, wouldn't it override this file on build?

ShyshkovOleg avatar Jul 22 '18 13:07 ShyshkovOleg

@ShyshkovOleg, I'm not using angular but anyway I put view.ios.js under git control, just to detect any overwriting. Of course the best solution would be fix this problem at the root but meanwhile...

drmistral avatar Jul 22 '18 13:07 drmistral

We're seeing this on Android when using the async pipe. A workaround we found was using the itemLoading event on ListView, and then do a markForCheck() inside a setTimeout.

So in template <ListView [items]="data | promisePipe | async" (itemLoading)="refresh()"></ListView> In TS file:

refresh() {
   setTimeout(() => this.changeDetectorRef.markForCheck(), 0); // Queue macro event
}

larssn avatar Nov 08 '18 10:11 larssn

Is there any news to this story? I'm having similar issues on IOS. I have tried so many different things now and nothing helps.

Kraften avatar Mar 05 '19 12:03 Kraften