Skip to content

Use IckleBot's Threading Model

sahan edited this page Mar 31, 2013 · 5 revisions

IckleBot provides an alternative simplified threading model which relies on annotations for the creation and execution of worker threads. In addition it exposes the UI event loop so that UI tasks may be executed with ease.

####1. Executing Asynchronous Tasks

Blocking operations which are to be executed on worker threads should be decoupled into a separate method and assigned a unique ID. These operations can then be invoked using IckleActivity's runAsyncTask() method.

@Layout(R.layout.act_profile)
@Title(R.string.ttl_act_profile)
public class ProfileActivity extends IckleActivity {

    private static final int ASYNC_UPDATE_PROFILE = 1;
    ...

    @Async(ASYNC_UPDATE_PROFILE)
    private void updateProfile(User user) {
    
        endpoint.updateProfile(user);
    }
    ...

    @Override
    protected void onStop() {

        super.onStop();
        runAsyncTask(ASYNC_UPDATE_PROFILE, this.user);
    }
    ...
}

####2. Executing UI Tasks

Posting UI events is performed in a similar manner using IckleActivity's runUITask() method. The following example shows how background and UI tasks can work in tandem.

@Layout(R.layout.act_profile)
@Title(R.string.ttl_act_profile)
public class ProfileActivity extends IckleActivity {

    private static final int ASYNC_UPDATE_PROFILE = 1;
    private static final int UI_SYNCED = 1;

    @InjectString(R.string.msg_synced)
    private String msgSynced;
    ...

    @Async(ASYNC_UPDATE_PROFILE)
    private void updateProfile(User user) {
    
        endpoint.updateProfile(user);
        runUITask(UI_SYNCED);
    }
    
    @UI(UI_SYNCED)
    private void onSynced() {    

        Toast.makeText(ProfileActivity.this, msgSynced, Toast.LENGTH_SHORT).show();
        Log.i(getClass().getSimpleName(), msgSynced); 
    }

    @Override
    protected void onStop() {

        super.onStop();
        runAsyncTask(ASYNC_UPDATE_PROFILE, this.user);
    }
    ...
}

Async and UI task IDs are independent of each other.

Clone this wiki locally