Giter VIP home page Giter VIP logo

Comments (8)

dpr-odoo avatar dpr-odoo commented on August 19, 2024

Can you post IncomingShipment -> ShipmentLoader : doInBackground() method logic ?

from framework.

Devang-Targetint avatar Devang-Targetint commented on August 19, 2024

Yes, .... here it is ...

public class IncomingShipment extends BaseFragment implements
OnItemLongClickListener, OnItemClickListener {

           @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
                      scope = new AppScope(getActivity());
                      ...................................
                   init();
                   return mIsView;
           }

           private void init() {
           ..............................................
              mShipmentLoader = new ShipmentLoader();
              mShipmentLoader.execute();
           ..............................................
      }

      private void checkStatus() {
          if (db().isEmptyTable()) {
                      scope.main().requestSync(IncomingShipmentProviders.AUTHORITY);
          }
      }

      class ShipmentLoader extends AsyncTask<Void, Void, Void> {

              @Override
              protected Void doInBackground(Void... params) {
                  db().getOEInstance().syncWithServer(); 

                  mIncomingShipmentObjects.clear();
                  OEUser user = OEUser.current(getActivity());
                  Integer uid = user.getUser_id();
                  mIncomingShipmentObjects.addAll(db().select("type = ? and state = ? and assignee_id = ?",
                                                          new String[] {"in","assigned",uid.toString()}));
                  db().close();
                  return null;
              }

              @Override
              protected void onPostExecute(Void result) {
                      mISListAdapter.notifiyDataChange(mIncomingShipmentObjects);
                      checkStatus();
              }
      }

    @Override
    public Object databaseHelper(Context context) {
        Log.d("Context", "context -- IncomingShipment -> databaseHelper -> " + context);
        if (context != null){
            mContextIS = context;
            Log.d("Context", "context -- IncomingShipment -> databaseHelper -> mContextIS = context " + context);
        } else {
            mContextIS = getActivity().getApplicationContext();
            Log.d("Context", "context -- IncomingShipment -> databaseHelper -> mContextIS = getactivity() " + mContextIS);
        }
        mContextIS = context;
            return new StockPickingDB(mContextIS);
    }

    SyncFinishReceiver mSyncFinishIs = new SyncFinishReceiver() {
            public void onReceive(Context context, android.content.Intent intent) {
                 mShipmentLoader = new ShipmentLoader();
                 mShipmentLoader.execute();
            };
    };

}

from framework.

dpr-odoo avatar dpr-odoo commented on August 19, 2024

Note that, you must have to return database model object in databaseHelper() (https://github.com/Odoo-mobile/framework/blob/1.0/src/com/odoo/addons/idea/Library.java#L179,L182) method in your IncomingShipment class. db() method takes object from databaseHelper()

Put all your code under runOnUiThread inside doInBackground()
(https://github.com/Odoo-mobile/framework/blob/1.0/src/com/odoo/addons/idea/Library.java#L107,L133)

scope.main().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if (db().isEmptyTable()) {
            db().getOEInstance().syncWithServer(); 
        }

        mIncomingShipmentObjects.clear();
        OEUser user = OEUser.current(getActivity());
        Integer uid = user.getUser_id();
        mIncomingShipmentObjects.addAll(db().select("type = ? and state = ? and assignee_id = ?",
                                                          new String[] {"in","assigned",uid.toString()}));
    }
});
return null;

And db().close() does not need to write. When sync finished it will automatically close database connection.

Hope this will work.

Regards,
DPR

from framework.

Devang-Targetint avatar Devang-Targetint commented on August 19, 2024

Hi Dharmang,

Thanks for fast reply. I will try that solution.

Here is databaseHelper()...

    @Override
    public Object databaseHelper(Context context) {
//        Log.d("Context", "context -- IncomingShipment -> databaseHelper -> " + context);
        if (context != null){
            mContextIS = context;
//            Log.d("Context", "context -- IncomingShipment -> databaseHelper -> mContextIS = context " + context);
        } else {
            mContextIS = getActivity().getApplicationContext();
//            Log.d("Context", "context -- IncomingShipment -> databaseHelper -> mContextIS = getactivity() " + mContextIS);
        }
        mContextIS = context;
            return new StockPickingDB(mContextIS);
    }

Here is StockPickingDB class

public class StockPickingDB extends OEDatabase {

        Context mContextSP = null;
        public StockPickingDB(Context context) {
                super(context);
//                Log.d("Context", "context -- StockPickingDB -> StockPickingDB -> " + context);
                mContextSP = context;
        }

        @Override
        public String getModelName() {
                return "stock.picking";
        }

        @Override
        public List<OEColumn> getModelColumns() {
//            Log.d("Context", "context mContextSP -- StockPickingDB -> getModelColumns -> " + mContextSP);
                List<OEColumn> cols = new ArrayList<OEColumn>();
                cols.add(new OEColumn("name", "name", OEFields.varchar(64)));
                cols.add(new OEColumn("date", "date", OEFields.datetime("dd/MM/yyyy HH:mm:ss")));
                cols.add(new OEColumn("partner_id", "Customer", OEFields.manyToOne(new ResPartnerDB(mContextSP))));
                cols.add(new OEColumn("assignee_id","assignee_id", OEFields.manyToOne(new ResUserDB(mContextSP))));
                cols.add(new OEColumn("move_lines","move_lines", OEFields.oneToMany(new StockMoveDB(mContextSP),"picking_id")));
                cols.add(new OEColumn("type", "type", OEFields.varchar(64)));
                cols.add(new OEColumn("state", "state", OEFields.varchar(64)));
                cols.add(new OEColumn("signature", "signature", OEFields.blob()));
                return cols;
        }
}

Do I need to add anything else to my database class?

from framework.

dpr-odoo avatar dpr-odoo commented on August 19, 2024

Hello,
Database class LGTM.

You don't need to check for context in databaseHelper()

Just,

@Override
public Object databaseHelper(Context context) {
    return new StockPickingDB(context);
}

To get context just cast getActivity() in your fragment under onCreateView()

mContextIS = (Context) getActivity();

from framework.

Devang-Targetint avatar Devang-Targetint commented on August 19, 2024

Hi Dharmang,

A million ton of thanks.
App is not crashing for the moment and becomes more faster than earlier ...

Thanks a lot.. 👍 🍸

from framework.

mohitt avatar mohitt commented on August 19, 2024

Hi @dpr-odoo,

Thanks a ton for your helping us. I am trying to help @Devang-Targetint . Since, the app is not responding.

After looking at the code, what I understood is that the framework is creating the database connection in the UI thread and if we use the AsyncTask's "doInBackground" to sync with server it would start throwing exception. Since, it will not be able to close the database connection. But if we use runOnUiThread inside doInBackground()
(https://github.com/Odoo-mobile/framework/blob/1.0/src/com/odoo/addons/idea/Library.java#L107,L133), Then it will be able to close the connection. Since it will do that on the same thread.

Now, the issue that we are facing is that we are downloading a good amount of data, that includes but is not limited to delivery order(stock.picking.out) stock_move (products of all the delivery orders) and all products serial numbers (stock_production_lot). So the time taken is decent, since this takes more than the expected time, app starts to show ANRs (Application Not Respoding) because we are doing it on the UI thread using runOnUiThread

So, this is a deadlock kind of situation if we only use doInBackground (not runOnUiThread ) then it will give us sql lite exception and if we use doInBackground (with runOnUiThread) then the App starts to show ANRs.

Do, let me know if you have any suggestions or questions?

Regards
Mohit Thakral

from framework.

dpr-odoo avatar dpr-odoo commented on August 19, 2024

Hello @codemerlin

If you are getting huge amount of data from server than just create one background sync service which handle your data.

as we have created for sync library data https://github.com/Odoo-mobile/framework/blob/1.0/src/com/odoo/addons/idea/Library.java#L111,L113

You should create your own sync service that will get data from server and after data load finish you can notify your adapter to display them https://github.com/Odoo-mobile/framework/blob/1.0/src/com/odoo/addons/idea/Library.java#L246,L257

Here is sync service example: https://github.com/Odoo-mobile/framework/blob/1.0/src/com/odoo/addons/idea/services/LibraryService.java

Register your provider and service in Android Manifest file : https://github.com/Odoo-mobile/framework/blob/1.0/AndroidManifest.xml#L83,L101

You can refer this application demo with documentation which shows how to create sync service
http://mobile.openerp.co.in/developing-first-app/index.html

Regards,
dpr

from framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.