https://developer.android.com/codelabs/basic-android-kotlin-training-repository-pattern#3
https://heegs.tistory.com/90
https://vagabond95.me/posts/android-repository-pattern/
https://devvkkid.tistory.com/196
(UI Layer와 Data Sources 사이에 Repository들이 위치)
( 클라이언트(=ViewModel) <--통신-- API(=Repository) --통신--> 서버(=data source) )
Repository는 data Sources(예: persistent model, Web services 및 Caches) 간의 충돌을 해결하고 이 data에 대한 변경 사항을 중앙 집중화할 수 있음.
아래 다이어그램은 Activity와 같은 App Component가 Repository를 통해 data sources와 상호 작용할 수 있는 방법을 보여줌.
이렇게 로직으로 수행될 수 있도록 하기 위해 Repository Pattern을 사용하는 것.
(Repository로 인해 Presentation Layer와 Data Layer의 coupling을 약하게 함)
Advantages of using a repository
Repository는 App의 Data의 특정 부분에 대한 신뢰할 수 있는 single source 역할을 해야 한다.
네트워크 리소스 및 오프라인 캐시와 같은 여러 data sources로 작업할 때 Repository는 App의 Data가 가능한 한 정확하고 최신 상태인지 확인하여 App이 오프라인일 때도 best possible experience를 제공한다.
캐시는 앱에서 사용하는 Data Storage를 의미.
예를 들어, 사용자의 인터넷 연결이 interrupt된 경우에 대비하여 네트워크의 Data를 일시적으로 save할 때 쓰인다.
캐시는 특정 작업에 따라 더 간단하거나 더 복잡한 다양한 형태를 취할 수 있음.
다음 표는 Android에서 네트워크 캐싱을 구현하는 여러 가지 방법이다.
Caching Technique | 용도 |
---|---|
Retrofit은 Android를 위한 type-safe REST client를 구현하는 데 사용되는 networking library. 모든 network 통신 결과의 copy를 로컬에 저장하도록 Retrofit을 구성할 수 있음. | simple requests와 responses 그리고 빈번한 network 호출, small dataset에 적합한 solution |
DataStore는 key-value Pairs를 저장하는데 유용 | App Setting과 같은 key-value에 유용한 solution. 큰 structured data에는 적합하지 않음 |
SQLite를 통해 abstraction layer를 제공하는 SQLite object-mapping library인 Room을 사용해 Data를 캐시할 수 있음. | 디바이스의 파일 시스템에 구조화된 데이터를 저장하는 가장 좋은 방법은 로컬 SQLite 데이터베이스에 있기 때문에 복잡한 구조화된 쿼리 가능 데이터(structured queryable data)에 권장되는 Solution |