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
ormservice != null
used, you're. may worry , addassert(mbound == mservice != null);
check.atomicity: in strict thread safe conditions,
onserviceconnected
may blocked beforembound = true;
, while in other thread,mservice
,mbound
may conflict in state.complexity: if else editing code modified
mbound
other state mistake?
hope help.
Comments
Post a Comment