2017-12-27 39 views
1
내가 이런 식으로 중포 기지 저장 장치를 사용하여 이미지 다운로드 해요

의 활동 파괴 후 호출됩니다의 onComplete은 FirebaseStorage

StorageReference storageReference = FirebaseStorage.getInstance().getReference(); 
storageReference.child("images/").child("myimage.jpg").getBytes(Long.MAX_VALUE).addOnCompleteListener(this); 

을 내가 알고 리스너를 추가 할 때 다운로드가 완료 :

@Override 
public void onComplete(@NonNull Task<byte[]> task) 
{ 
    //stuff... 
} 

이 문제는 인터넷을 제거하고 이미지를 다운로드하고 활동을 삭제하려고 할 때 발생합니다. 액티비티가 파괴 되었기 때문에 콜을 파괴해야하지만 콜은 저장되어있다. 실제로 인터넷을 활성화하면 활동이 소멸 되었음에도 불구하고 onComplete 메서드가 호출됩니다. 이 마지막 이 다시 생성되지 않았습니다. 로그를 모두 확인했습니다. 그렇다면 전화를 확실히 중단하려면 어떻게해야합니까? 그리고 그 방법은 왜 불려지나요?

+0

"완료 청취자의 ". 활동이 가비지 수집되지 않기 때문에 실제로 메모리에 꽤 나쁜 것입니다. 청중이 파괴되면 활동을 제거해야합니다. –

답변

1

StorageReference.html#getFile 메서드를 사용하면 취소 가능 FileDownloadTask을 사용할 수 있습니다.
https://developers.google.com/android/reference/com/google/firebase/storage/StorageReference
https://developers.google.com/android/reference/com/google/firebase/storage/FileDownloadTask
https://developers.google.com/android/reference/com/google/firebase/storage/FileDownloadTask.TaskSnapshot

희망 :

, 당신은 onCreate()에 전화를하고 onDestroy() 방법에 취소 할 전화 말을 여기에 문서의

/** 
* reference to the download file task 
*/ 
FileDownloadTask downloadTask; 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // get the StorageReference to the images folder 
    StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images/"); 
    // create the download file task 
    downloadTask = storageReference.getFile(new File("myimage.jpg")); 
    // this activity should also implement OnCompleteListener<FileDownloadTask.TaskSnapshot> 
    downloadTask.addOnCompleteListener(this); 
} 

@Override 
protected void onDestroy() { 
    if (downloadTask != null) { 
     downloadTask.cancel(); 
     downloadTask = null; 
    } 
    super.onDestroy(); 
} 

더 많은 정보를 수 있습니다 이것은 도움이됩니다.

2

작업 API에 등이 있습니다. addOn[...]Listener(Activity, On[...]Listener) 변형을 사용하십시오. 그러면 청취자는 액티비티의 onStop() 메소드에서 자동으로 제거됩니다. 귀하의 경우, 그것은 다음과 같을 것이다 :

여기
...addOnCompleteListener(this, this); 

documentation. 당신처럼 취급 다른 스레드에서 Activity` 객체 '에 대한 강한 메모리 참조를 유지하기 때문에 귀하의 메서드를 호출 할