Reputation: 39
I am observing a list of objects in LiveData
I have removed the observers manually but the it is not removed. It is getting called again after a change. Here is the code. Here repo.
getKeyFor' returns LiveData<List<ImageAddress>>
against the arguments passed. ImageAddress is the class I created.
repo.getKeyFor(fname).observe(this, new Observer<List<ImageAddress>>() {
@Override
public void onChanged(List<ImageAddress> imageAddresses) {
try{
Log.d(TAG, "onChanged: decrpted Image inside :"+fname);
ImageAddress imageAddress=imageAddresses.get(0);
String strKey=imageAddress.getKey();
Log.d(TAG, "onChanged: IMAGE address:"+imageAddresses.get(0).getAddress()+", "+imageAddress.getKey());
SecretKey k=convertKey(strKey);
byte[] decryptedImgArray=cryptoChaCha20.decrypt(imgArr,k);
String decFile=getChangedFileName(rawFilePath,"Decrypted");
saveFile(decFile,decryptedImgArray);
Log.d(TAG, "onChanged: decrpted Image inside :"+fname);
Toast.makeText(getApplicationContext(),"File Decrypted! Saved to gallery!",Toast.LENGTH_SHORT).show();
if(isImageFile(decFile)){
Bitmap bitmap = BitmapFactory.decodeByteArray(decryptedImgArray , 0, decryptedImgArray.length);
imageView.setImageBitmap(bitmap); }
}
catch (Exception e){
e.printStackTrace();
finish();
}
finally{
repo.getKeyFor(fname).removeObservers(MainActivity.this);}
}
});
code for getKeyFor()
LiveData<List<ImageAddress>> getKeyFor(String address){ return dao.getKeyFor(address);}
Upvotes: 0
Views: 142
Reputation: 1850
Note: Assuming that dao is genereted by room, the method getKeyFor will return a new instance of LiveData in each call.
Upvotes: 1