[Android] Lazy Loading

Android 2012. 6. 29. 18:01


Lazy Loading 처리


Thread로 UI갱신을 하려고 하면 예외(※1)가 발생한다.

그러므로 Thread에서 UI의 갱신을 할수 있는 Handler를 호출.

Thread에서 약간의 딜레이(sleep)을 두지 않으면, 메인의 UI가 반응하지 않는 현상이 있었다.


※1 ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views





//UI갱신을 위한 핸들러

final Handler handler = new Handler() {

  

public void handleMessage(Message msg) {

 

Bundle bundel = msg.getData();

 

int i = bundel.getInt("i");

 

// Lazy Loading 처리 구문을 여기에.

// UI는 Class.this로 직접 지시.

 

}

}; 


//쓰레드 처리를 위한 클래스

class LazyLoad extends Thread{

Handler mHandler; //UI접근을 위한 핸들러

int length// 반복수 


public LazyLoad(int length, Handler handler){

this.mHandler = handler;

this.length = length;

}


public void run(){



for(int i=0; i<this.length;i++){


Bundle bundle = new Bundle();

bundle.putInt("i", i);

Message msg = mHandler.obtainMessage();

msg.setData(bundle);

mHandler.sendMessage(msg);

this.sleep(100);

}


}

}


추가. 스레드에서 for문을 돌리는 것이 무식한 짓이었던 듯. 각 로딩당 스레드를 생성하는 것이 나을 듯하다. 퍼포먼스의 저하가 우려된다면, 스레드 작업관리 큐를 만들어 처리하는 것이 좋을 듯.



Posted by tenn
,