Skip to content

Commit

Permalink
Updated documentation to reflect annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick Brock committed Nov 15, 2014
1 parent 91a7980 commit 3b66bcd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
27 changes: 14 additions & 13 deletions HOWTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MessageEvent {
```
### 2: Prepare subscribers ###

Subscribers implement event handling `onEvent` methods that will be called when an event is received. They also need to register and unregister themselves to the bus.
Subscribers implement event handling methods that will be called when an event is received. These are defined with the ``@Subscribe`` annotation. They also need to register and unregister themselves to the bus.

```java
@Override
Expand All @@ -35,11 +35,13 @@ Subscribers implement event handling `onEvent` methods that will be called when
}

// This method will be called when a MessageEvent is posted
public void onEvent(MessageEvent event){
@Subscribe
public void onMessageEvent(MessageEvent event){
Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}

// This method will be called when a SomeOtherEvent is posted
@Subscribe
public void onEvent(SomeOtherEvent event){
doSomethingWith(event);
}
Expand All @@ -58,11 +60,12 @@ EventBus can handle threading for you: events can be posted in threads different

A common use case is dealing with UI changes. In Android, UI changes must be done in the UI (main) thread. On the other hand, networking, or any time consuming task, must not run on the main thread. EventBus helps you to deal with those tasks and synchronize with the UI thread (without having to delve into thread transitions, using AsyncTask, etc).

In EventBus, you may define the thread that will call the event handling method `onEvent` by using a **ThreadMode**:
In EventBus, you may define the thread that will call the event handling method by using a **ThreadMode**:
* **PostThread:** Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers using this mode should return quickly to avoid blocking the posting thread, which may be the main thread.
Example:
```java
// Called in the same thread (default)
@Subscribe
public void onEvent(MessageEvent event) {
log(event.message);
}
Expand All @@ -71,26 +74,29 @@ Example:
Example:
```java
// Called in Android UI's main thread
@Subscribe(threadMode = ThreadMode.MainThread)
public void onEventMainThread(MessageEvent event) {
textField.setText(event.message);
}
```
* **BackgroundThread:** Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread that will deliver all its events sequentially. Event handlers using this mode should try to return quickly to avoid blocking the background thread.
```java
// Called in the background thread
@Subscribe(threadMode = ThreadMode.BackgroundThread)
public void onEventBackgroundThread(MessageEvent event){
saveToDisk(event.message);
}
```
* **Async:** Event handler methods are called in a separate thread. This is always independent from the posting thread and the main thread. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
```java
// Called in a separate thread
@Subscribe(threadMode = ThreadMode.Async)
public void onEventAsync(MessageEvent event){
backend.send(event.message);
}
```

*Note:* EventBus takes care of calling the `onEvent` method in the proper thread depending on its name (onEvent, onEventAsync, etc.).
*Note:* EventBus takes care of calling the subscribing method in the proper thread depending on its annotations threadMode argument (threadMode=ThreadMode.Async, MainThread, etc...).

Subscriber priorities and ordered event delivery
------------------------------------------------
Expand Down Expand Up @@ -164,7 +170,8 @@ After that, a new Activity gets started. During registration using registerStick
super.onStart();
EventBus.getDefault().registerSticky(this);
}


@Subscribe(threadMode = ThreadMode.MainThread)
public void onEventMainThread(MessageEvent event) {
textField.setText(event.message);
}
Expand All @@ -185,9 +192,8 @@ ProGuard configuration
----------------------
ProGuard obfuscates method names. However, the onEvent methods must not renamed because they are accessed using reflection. Use the following snip in your ProGuard configuration file (proguard.cfg):
<pre><code>-keepclassmembers class ** {
public void onEvent*(**);
}
</code></pre>
@de.greenrobot.event.annotations.Subscribe public *;
}</code></pre>


Comparison with Square's Otto
Expand All @@ -199,11 +205,6 @@ Otto is another event bus library for Android; actually it's a fork of Guava's E
<th>EventBus</th>
<th>Otto</th>
</tr>
<tr>
<th>Declare event handling methods</th>
<td>Name conventions</td>
<td>Annotations</td>
</tr>
<tr>
<th>Event inheritance</th>
<td>Yes</td>
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ EventBus...

[![Build Status](https://travis-ci.org/greenrobot/EventBus.svg?branch=master)](https://travis-ci.org/greenrobot/EventBus)

EventBus in 3 steps
EventBus in 4 steps
-------------------
1. Define events:<br/>
<code>public class MessageEvent { /* Additional fields if needed */ }</code><br/><br/>
2. Prepare subscribers:<br/>
<code>eventBus.register(this);</code><br/>
2. Register your subscriber (in your onCreate or in a constructor):<br/>
<code>eventBus.register(this);</code><br/><br/>
3. Declare your subscribing method<br/>
<code>@Subscribe</code><br/>
<code>public void onEvent(AnyEventType event) {/* Do something */};</code><br/><br/>
3. Post events:<br/>
4. Post events:<br/>
<code>eventBus.post(event);</code>

Add EventBus to your project
Expand Down Expand Up @@ -54,8 +56,7 @@ Details on EventBus and its API are available in the [HOWTO document](HOWTO.md).
Additional Features and Notes
-----------------------------

* **NOT based on annotations:** Querying annotations are slow on Android, especially before Android 4.0. Have a look at this [Android bug report](http://code.google.com/p/android/issues/detail?id=7811).
* **Based on conventions:** Event handling methods are called "onEvent".
* **Based on annotations:** Event handling methods can be named however you want, and only need to be annotated with **@Subscribe**.
* **Performance optimized:** It's probably the fastest event bus for Android.
* **Convenience singleton:** You can get a process wide event bus instance by calling EventBus.getDefault(). You can still call new EventBus() to create any number of local busses.
* **Subscriber and event inheritance:** Event handler methods may be defined in super classes, and events are posted to handlers of the event's super classes including any implemented interfaces. For example, subscriber may register to events of the type Object to receive all events posted on the event bus.
Expand Down

0 comments on commit 3b66bcd

Please sign in to comment.