SeSAC OOD 11/27

gaebokchi·2021년 11월 27일
0

OOD

목록 보기
7/7

오늘의 목표는
1. collectionView Cell에 들어갈 image 서버통신
2. 상세페이지 UI
3. 상세페이지 서버통신

KingFisher 캐시

클로저 헤드를 살펴보니 Result<ImageCacheResult, KingfisherError>
.success 시 ImageChacheResult, .failure 시 KingFisherError

func setKFImage(from urlString: String) {
        let cache = ImageCache.default
        cache.retrieveImage(forKey: urlString) { result in
            switch result {
            case .success(let response):
                if response.image != nil {
                    self.image = response.image
                } else {
                    self.kf.setImage(with: URL(string: urlString))
                }
            case .failure(let err):
                print(err.errorCode)
            }
        }
    }

이미 캐시에 이미지가 있다면 그 이미지 사용, 캐시에 이미지 저장된 적 없다면 킹피셔의 setImage를 통해 url을 이미지로 변환

서버통신 후 갑자기 이상해진 CollectionView height

서버 통신 전에는 잘만 뜨던 콜렉션뷰가 서버를 붙이니 collectionView height이 엄청 줄어들었다.

스크롤뷰 위에 콜렉션뷰를 올려서 전체 스크롤이 될 수 있도록 써주었던 아래 코드의 위치가 잘못되었을 것이라 판단. 원래는 이 코드를 ViewDidLoad()에 넣어주었음

self.collectionViewHeight.constant = self.certiCollectionView.contentSize.height

서버통신 후 collectionView Cell에 들어갈 배열이 채워지기 때문에 프로퍼티 옵저버를 사용해서 배열이 바뀐 직후 호출되는 didSet()에 reloadData()를 넣어주었었다. 여기로 옮겨주니 잘됨

var certiListData: [CertiListData] = [] {
        didSet {
            certiCollectionView.reloadData()
            DispatchQueue.main.async {
                self.collectionViewHeight.constant = self.certiCollectionView.contentSize.height
            }
        }
    }

비슷한 이슈

certiDetail 메서드는 서버통신 메서드, 서버통신으로 정보를 가져온 후 다음 코드에서 가져온 정보들을 띄우려고 이렇게 작성했다. 근데 정보가 안띄워짐

var certiDetailData: CertiDetailData? {
        didSet {
            if let detailData = self.certiDetailData, let certi = detailData.certi {
                print(certi)
                setLabel(certi: certi)
                setImageView(imageUrl: certi.certiImage ?? "")
            }
        }
    }

그래서 결과를 담는 구조체의 프로퍼티 옵저버 didSet에 넣어주니 잘뜸

로그 찍어보니 바로 이해가 갔다. 바보~

오늘 목표했던 것은 다 끝냈다 ~

0개의 댓글