nativescript-community / ui-collectionview

Allows you to easily add a collection view (grid list view) to your projects. Supports vertical and horizontal modes, templating, and more.
Apache License 2.0
59 stars 18 forks source link

iOS crash when async loading items into ObservableArray #55

Closed felixkrautschuk closed 1 year ago

felixkrautschuk commented 1 year ago

Make sure to check the demo app(s) for sample usage

Make sure to check the existing issues in this repository

If the demo apps cannot help and there is no issue for your problem, tell us about it

Please, ensure your title is less than 63 characters long and starts with a capital letter.

When loading data items asynchronously (REST api, ...) and pushing them into the ObservableArray of the collectionview, the app crashes on iOS with the following message: Uncaught Error: attempt to insert item 0 into section 0, but there are only 0 sections after the update

When I add the items synchronously (without any delay), the crash does not occur.

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

Is there any code involved?

XML

<cv:CollectionView loaded="onCollectionViewLoaded"
                           items="{{ items }}"
                           itemTap="onItemTap"
                           visibility="{{ isLoading ? 'collapse' : 'visible' }}">
            <cv:CollectionView.itemTemplate>
                <StackLayout padding="12" borderBottomWidth="1" borderBottomColor="#656565">
                    <Label fontSize="20" text="{{ id }}"/>
                    <Label fontSize="15" text="{{ title }}" textWrap="true"/>
                </StackLayout>
            </cv:CollectionView.itemTemplate>
</cv:CollectionView>

TS

...
const myItems = [{ id: 1, title "My title 1" }, { id: 2, title "My title 2" }, ... }];
vm.set("isLoading", true);

setTimeout(() => {
      vm.get("items").splice(0, vm.get("items").length);
      vm.get("items").push(...myItems);

      vm.set("isLoading", false);
, 2000);
...

Full sample app: ns-collectionview.zip

farfromrefug commented 1 year ago

@felixkrautschuk thanks for the great repro example! i have been hunting this one for a while. The fix has been published.

felixkrautschuk commented 1 year ago

@farfromrefug now it is working, thank you for the fix!