android - Is it relevant to keep a mBound flag when binding a service? -


as explained in documentation regarding bound services, mbound boolean used know whether service bound in activity. here excerpt of code example given in documentation:

public class bindingactivity extends activity {   localservice mservice;   boolean mbound = false;    private serviceconnection mconnection = new serviceconnection() {     public void onserviceconnected(componentname classname, ibinder service) {       localbinder binder = (localbinder) service;       mservice = binder.getservice();       mbound = true;     } } 

why use additional member rather setting mservice null if not bound? seems redundant me, , potentially error-prone.

there no need keep additional flag in activity.

additional flag adds risk on data consistency , atomicity, such as:

  • data consistency: else modify code may confused on should mbound or mservice != null used, you're. may worry , add assert(mbound == mservice != null); check.

  • atomicity: in strict thread safe conditions, onserviceconnected may blocked before mbound = true;, while in other thread, mservice , mbound may conflict in state.

  • complexity: if else editing code modified mbound other state mistake?

hope help.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -