I am using navigation component in my android app and I have a fragment that I need to auto sync data from server and store in room database which works fine (My idea is to make sure the room database at all time it has updated products that is incase a product was updated from the server it should pick when user start using the app) but I have this weird behavior when application start sync and i put it to background and resume again. If I leave the sync process to complete without from from the app it works perfectly but if I exit the app and come back again its messed up here is what I mean. When I sync data without exiting the app here is the output.
and when I exit the app while syncing data then resume again here is what am getting
it like when I resume the app the thread is restarting again instead of picking where it left.
This is how am calling my sync fragment from main activity on Resume method.
and here is my fragment code that sync data. NB I dont want to use AsyncTask as I saw it looks deprecated.
public class FragmentAutoSync extends Fragment {
FragmentAutoSyncBinding binding;
public FragmentAutoSync() {
// Required empty public constructor
}
List<ProductEntity> new_products;
List<ProductEntity> deleted_products;
private ProductRepository repository;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding=FragmentAutoSyncBinding.inflate(inflater);
repository=new ProductRepository(getActivity());
try {
progress_dialog= AlertUtil.getCustomDialog(getActivity(), R.layout.progress_layout);
progress_dialog.setCancelable(false);
progress_dialog.show();
handler=new Handler();
autoSyncService= Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
autoSyncService.execute(new ProductSyncTask());
} catch (Exception e) {
ExceptionHandler.logException(e);
ExceptionHandler.showToast(getActivity(),"Error occurred while syncing data");
}
return binding.getRoot();
}
Dialog progress_dialog;
private ExecutorService autoSyncService;
private Handler handler;
private class ProductSyncTask implements Runnable
{
@Override
public void run() {
startAutoSync();
}
}
void startAutoSync()
{
try{
if(new_products==null)
{
new_products=new ArrayList<>();
}
if(deleted_products==null)
{
deleted_products=new ArrayList<>();
}
TextView textView=progress_dialog.findViewById(R.id.text_progress);
textView.setVisibility(View.VISIBLE);
TextView text_processing=progress_dialog.findViewById(R.id.text_processing);
startSync(textView,text_processing);
}
catch (Exception e)
{
ExceptionHandler.logException(e);
ExceptionHandler.showToast(getActivity(),"Error Occurred while Syncing products");
}
finally {
handler.post(()->
{
close();
});
}
}
volatile int counter=0;
volatile int total;
void startSync(TextView textView,TextView text_processing) throws Exception
{
counter=1;
handler.post(()->{
text_processing.setText("Reading data...");
});
new_products=loadList(textView);
total=new_products.size()+deleted_products.size()+1;
handler.post(()->{
text_processing.setText("Syncing Data Please wait...");
});
repository.deleteAll();
;
for(ProductEntity p:new_products)
{
Thread.sleep(5);
repository.insertProduct(p);
handler.post(() -> {
textView.setText(counter+"/"+total);
counter++;
});
}
//deleted_products=AppUtil.deleted_products
for(ProductEntity e:deleted_products)
{
repository.deleteOneProduct(e);
textView.setText(counter+"/"+total);
counter++;
}
}
int i=0;
List<ProductEntity> loadList(TextView textView)
{
new_products= new ArrayList<>();
ProductEntity p;
i=0;
int counter=0;
for(int x=1;x<5000;x++) {
if(counter>=1000)
{
System.gc();
counter=0;
}
counter++;
p= new ProductEntity();
p.setBuying_price_per_item(0);
p.setMake("NA");
p.setName(randomName());//method to generate random name for simulating
p.setProduct_description(randomName());
p.setRetail_selling_price(450);
p.setTax(0);
p.setUnits("NA");
p.setQuantity_to_sell_wholesale(0);
p.setStatus("A");
p.setProduct_id(x);
new_products.add(p);
handler.post(()->
{
i++;
textView.setText("Read :"+String.valueOf(i));
}
);
}
return new_products;
}
}