Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

second collectionView.reloadData() hides collectionView #44

Closed
isayeter opened this issue May 20, 2021 · 3 comments
Closed

second collectionView.reloadData() hides collectionView #44

isayeter opened this issue May 20, 2021 · 3 comments

Comments

@isayeter
Copy link

Hello there,

I'm refreshing my collectionView whenever a data is supplied by the viewModel.

viewModel.listHomePage.asObservable().subscribe(onNext: { [weak self] responseModel in
    if responseModel != nil , let safeSelf = self {
        DispatchQueue.main.async {
            safeSelf.collectionView.reloadData()
        }
    }
}).disposed(by: disposeBag)

This observable is fired twice by our app cache mechanism.

First fire is occuring immediately by the latest cached remote server data (if exists),
Second fire is occuring by the network request with the latest remote server data when the request is finished.

When first fire is occured, I see everything is fine, my collection view is there, however when the network request finish and the second fire is occured, the collectionview is suddenly disappearing from the screen, it comes to visible until I physically touch anywhere in the collection view.

When I search if there is a similar issue, I found your reply:

#26 (comment)

then, I have changed my code to:

viewModel.listHomePage.asObservable().subscribe(onNext: { [weak self] responseModel in
    if responseModel != nil , let safeSelf = self {
        DispatchQueue.main.async {
            safeSelf.collectionView.reloadData()

            safeSelf.collectionView.performBatchUpdates({
                safeSelf.influencersCollectionView.collectionViewLayout.invalidateLayout()
             })

        }
    }
}).disposed(by: disposeBag)

Now, when the second fire is occured, I see collectionView refresh itself and in a moment it is going completely and recoming to screen in a very short time. (because we invalidate all layout). It is okey now, but it is still annoying to see collectionview is blinking.

Is it normal to see this problems when the second reloadData called? You have wrote that we don't need to call invalidateLayout anymore, but I'm facing this error if I don't use it, that's why I'm creating this issue.

Thank you.

@amirdew
Copy link
Owner

amirdew commented May 20, 2021

Hi,
I wasn't aware of this issue, I'll look into it soon, thanks for reporting.
I mentioned:

a bounds observer added so, you don't need to call "invalidateLayout" on "viewDidLayoutSubviews"

this might be a different issue.

@amirdew
Copy link
Owner

amirdew commented May 20, 2021

Oh I just realised the reason you see blinking is that in the new version (1.0.0)
the alpha layout attribute has been set to 0 until the UICollectionViewCell is loaded. this prevents showing the cell before applying the transforms and effects.
You can disable this by

private let layout = CollectionViewPagingLayout()

layout.transparentAttributeWhenCellNotLoaded = false

let me know how it works for you


and this is not necessary, just for simplicity you can call

layout.invalidateLayoutInBatchUpdate()

instead of

collectionView.performBatchUpdates({
     collectionView.collectionViewLayout.invalidateLayout()
})

@isayeter
Copy link
Author

yes, that's worked! thank you so much.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants