Giter Site home page Giter Site logo

rxsidedishes's Introduction

RxSideDishes

1. 메인 화면

rxsidedishes's People

Contributors

settpark avatar

Watchers

 avatar

rxsidedishes's Issues

[ImageCahce] 하나의 요소가 observable이 아닐 때

  • DTO는 [banchanDTO] 타입이고, Image는 observable 타입이었다.
  • flatMap을 쓰자니 DTO가 이벤트로 안 내려가고, 그냥 초기화를 해주자니, Image의 요소를 가져올 수 없었다.
  • 따라서 DTO의 모든 인자를 Observable로 초기화하여, Observable, Observable를 동시에 내려서 처리하였다. 초기화 하는 함수는 아래와 같다.
let temp = DTO.map {(DTO: Observable<BanchanDTO>.just($0), image: imageManager.getCachedImage(url: $0.image))}
  • 그 후에 flatMap, combineLatest를 활용하여 Banchan을 초기화 하였는 데, combineLatest보다 더 적절한 함수가 있을 거 같다. 아직 찾지 못하였다.
return Observable.from(temp)
    .flatMap {
        Observable.combineLatest($0, $1) { dto, image -> Banchan in
            return Banchan.init(hash: dto.detailHash, image: image, alt: dto.alt, deliveryType: dto.deliveryType, title: dto.title, description: dto.description, nPrice: dto.nPrice, sPrice: dto.sPrice, badge: dto.badge)
        }
    }.buffer(timeSpan: .never, count: 0, scheduler: MainScheduler.instance)
  • buffer의 잘못된 사용
    1. combineLatest { }.buffer를 사용하여 이벤트가 하나씩 전달되는 문제를 겪었다. 계속 이벤트가 변하기 때문에, 시뮬레이터에 마지막 요소만 표시되었다.
    2. flatMap{ }.buffer로 올바른 위치에 구현하였으나, count를 DTO.count로 두어 시뮬레이터에 아무것도 표시되지 않는 문제를 확인하였다. 이벤트가 오지 않을 때까지 버퍼로 끊어서 보내는 듯 하고, 한번 버퍼가 차서 이벤트를 송신한 후에 다음 이벤트를 한번 더 확인하는 함수인 듯 하다.

[API] ImageCache 동작 방식 변경과정에서 Image 표시의 문제

Issue: Image 시뮬레이터에 표시 되지 않음.

이미지 캐싱 변경 과정


  1. 기존의 NScahe의 Singleton, UIimageView extension으로 해결하던 역할을 객체에게 할당하고 이를 Service layer에서 처리하도록 함. 새로운 ImageCacheManager는 아래와 같음(NSCache를 내부 속성으로 가지고 있음)
class ImageCacheManager: ImageCacheManagble {
    
    let cacheManager: NSCache<NSString, UIImage>
    
    init() {
        cacheManager = NSCache<NSString, UIImage>()
    }
}

  1. Service layer에서 url을 통해 image를 fetch하고 이를 캐싱하고자 함. image가 repo layer에 넘어가기 전에 persistence(core data)에 저장해주고자 1과 같이 의도함. 의도대로 작성한 코드는 아래와 같음
func fetchAndCacheImage(url: String) -> UIImage {
    let cacheKey = NSString(string: url)
    var resultImage = UIImage()
    
    if let cachedImage = cacheManager.object(forKey: cacheKey) {
        return cachedImage
    }
    
    if let url = URL(string: url) {
        URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
            if let _ = error {
                resultImage = UIImage()
            }
            if let data = data, let image = UIImage(data: data) {
                self?.cacheManager.setObject(image, forKey: cacheKey)
                resultImage = image
            }
        }
        return resultImage
    }
    return resultImage
}

  1. Image는 decodable할 수 없기에 banchan을 아래와 같이 새로 규정함. 기존의 banchan을 banchanDTO로 수정함.
struct Banchan: Equatable {
    
    let detailHash: String
    let image: UIImage
    let alt: String
    let deliveryType: [String]
    let title: String
    let description: String
    let nPrice: String?
    let sPrice: String
    let badge: [String]?
    
    static var empty = Banchan.init()
    
    init() {
        detailHash = ""
        image = UIImage()
        alt = ""
        deliveryType = []
        title = "빈 더미 더미"
        description = ""
        nPrice = ""
        sPrice = ""
        badge = []
    }
    
    init(hash: String, image: UIImage, alt: String, deliveryType: [String], title: String, description: String, nPrice: String?, sPrice: String, badge: [String]?) {
        self.detailHash = hash
        self.image = image
        self.alt = alt
        self.deliveryType = deliveryType
        self.title = title
        self.description = description
        self.nPrice = nPrice
        self.sPrice = sPrice
        self.badge = badge
    }
}

  1. banchanDTO를 banchan으로 변경하는 코드를 작성함.
func makeBanchan(from DTO: Banchans) -> [Banchan] {
    var banchans: [Banchan] = []
    for ele in DTO.body {
        let fetchImage = self.imagecacheManager.fetchAndCacheImage(url: ele.image)
        let makedbanchan = Banchan.init(hash: ele.detailHash, image: fetchImage, alt: ele.alt,
deliveryType: ele.deliveryType, title: ele.title, description: ele.description, nPrice:
ele.nPrice, sPrice: ele.sPrice, badge: ele.badge)
        banchans.append(makedbanchan)
    }
    return banchans
}

의심 사항

  1. 위의 2번 코드가 잘못 구현 되었다. (제일 가능성 높음)
  2. 4에서 [banchan]을 생성하는 과정에서 잘못 되었다. (근데 텍스트는 표시 됨)
  3. service영역에서 repo로 데이터는 잘 전달 되었으나, repo -> viewmodel -> viewcontroller에서 데이터가 잘못 추합되거나 오류가 발생했다.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.