2016-06-20 1 views
1

저는 editText 및 recyclerView에서 작업하고 있습니다. 내 EditText에 글자를 쓸 때 내 recyclerView가 업데이트되었습니다.누출 : 타이머 및 텍스트 워처

사용자가 문자를 쓸 때마다 요청을 보내지 않으려면 Timer를 textWatcher에 넣습니다.

searchDestinationEt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { 
      //There is nothing to do here 
     } 

     @Override 
     public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { 
      if (timer != null) { 
       timer.cancel(); 
      } 
     } 

     @Override 
     public void afterTextChanged(final Editable s) { 

      timer = new Timer(); 

      //we schedule this in order to avoid sending useless request. 
      //We wait the user is finishing writing before sending requests 
      timer.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        ((Activity) context).runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          actionsListener.onDestinationSearch(s.toString()); 
         } 
        }); 
       } 
      }, DELAY_SEND_REQUEST); 
     } 
    }); 

잘 작동하지만 leakcanary는이 코드 부분에 누수가 있다고 말합니다. 아이디어가 있으십니까? ?

+0

http://www.mopri.de/2010/timertask-bad-do-it-the-android -way-use-a-handler/ – oiZo

+0

@oiZo Thx, 시도하겠습니다. 그러나 거기에 누수가 왜 존재 하는지를 설명하지는 못합니다 : /. – Bob

답변