Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
public class MainActivity extends BaseActivity<ActivityMovieBinding, MainPresenter> implements MainContract.View {

private MovieListAdapter adapter;
private LinearLayoutManager mLinearLayoutManager;
private final int UNVISIBLE = 5;

@Override
protected int getLayoutId() {
Expand Down Expand Up @@ -48,8 +50,9 @@ protected void onCreate(Bundle savedInstanceState) {

private void initView() {

mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// recyclerView 생성
binding.recyclerMovie.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
binding.recyclerMovie.setLayoutManager(mLinearLayoutManager);
binding.recyclerMovie.setAdapter(adapter);
binding.recyclerMovie.setEmptyView(binding.emptyView);
binding.recyclerMovie.setNestedScrollingEnabled(false);
Expand All @@ -60,7 +63,11 @@ private void initView() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(!binding.recyclerMovie.canScrollVertically(1)){

int lastVisibleItemPos = mLinearLayoutManager.findLastVisibleItemPosition();
int itemCount = adapter.getItemCount();

if((itemCount > 0) && (UNVISIBLE + lastVisibleItemPos) > itemCount ){
presenter.loadItems(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MainPresenter implements MainContract.Presenter {
private AdapterContract.Model<Movie> adapterModel;

private String searchKey = ""; // 검색 키워드
private final int PAGE_UNIT = 20; // 한번에 가져올 데이터 개수
private final int PAGE_UNIT = 10; // 한번에 가져올 데이터 개수

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20에서 10으로 변경된 이유가 궁금합니다.

@nusurprise nusurprise Jan 20, 2019

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20개씩 호출은 마지막 5번째 아이템까지 닿는데 여러 번 스크롤을 해야 합니다. 스크롤을 두세 번만 내리면서 API가 연속적으로 잘 호출되는지 확인하고 싶었습니다. 감사합니다!

private int currentPage = 0; // 현재 페이지 index
private boolean isEndOfPage = false; // 페이지 끝 flag

Expand All @@ -41,10 +41,9 @@ public void onViewCreated() {
public void loadItems(boolean isRefresh) {

// refresh true 의 경우 초기화
if (isRefresh){
if (isRefresh) {
currentPage = 0;
isEndOfPage = false;
adapterModel.clearItems();
}

// 마지막 페이지가 아니고 로딩중 아닌 경우 getMovieList 호출
Expand Down Expand Up @@ -83,6 +82,11 @@ private void getMovieList(){
// 로딩 flag OFF
isLoading.set(false);

// 검색버튼에 의한 호출일 경우, 기존 list 비우기
if(currentPage == 1) {
adapterModel.clearItems();

@park-ju1008 park-ju1008 Jan 19, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRefresh 조건 안에서 기존 list를 비우지 않고 api 호출후 하는 이유가 궁금합니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

검색 API 호출은 비동기적으로 구현되어 있기 때문에 공유자원(itemList)에 대한 Race Condition 문제가 발생할 수 있습니다.
이를 해결하기 위해 해당 코드는 비동기 호출이 완료될 때까지 공유자원(itemList)에 대한 점유권(쓰기 작업)을 호출 원인(검색 버튼 또는 스크롤링)이 소유하도록 isLoading 변수를 사용하여 임계 영역을 구현했습니다.

CallBack 내부로 itemList 갱신 코드를 옮긴 것은 공유자원에 대한 접근 코드를 임계 영역으로 옮긴 것이 됩니다.
적고 보니 isLoading.set(false)를 모든 작업을 마친 뒤에 호출하는 것이 옳아 보입니다. 감사합니다!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isLoading.set(false)의 위치에 따라 성능적으로 어떻게 차이가 나는지 알 수 있을까요?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 섣불리 판단했습니다..
마지막에 배치한 의도는 데이터 처리까지 완료한 후에 ProgressBar 를 중단 시키려고 했습니다,
하지만 검색 결과가 없을 시에 중간에 return 구문으로 종료함으로써 계속해서 ProgressBar 가 표시되는 문제가 발생합니다. 지적해주셔서 감사합니다!

}

List<Movie> movieList = response.getMovieList();

if(movieList.size() == 0){ // 검색 결과가 없는 경우
Expand Down