推荐 greenrobot eventbus,简化安卓开发,提高安卓维护性,优化安卓性能
最近在研究移动开发,广泛的阅读有关cordova,apicloud,android资料。发现安卓的开发还是很简单的。再发现greenrobot eventbus开源框架不仅可以简化安卓开发,有可以大幅度的提高安卓的维护性,性能也能有所提高。开发安卓的难度觉得比windows下的winform很简单。
greenrobot eventbus的开源地址在https://github.com/greenrobot/EventBus,如果你熟悉android的开发,你一定觉得greenrobot eventbus是好东西,疯狂的爱上她。
推荐greenrobot eventbus 的理由
我是从事过.net平台下的开发,在项目中经常碰到对象相互依赖的问题,项目越大,对象越多,对象图就越复杂,从而导致了项目的维护性非常困难。对象调用另外一个对象本质就是发送消息。基于事件驱动的编程时,对象调用另一个对象的方法时,不必保存另一个对象的引用,而是订阅另一个对象发布的消息。这样就解决庞大的对象图造成的项目可读性差,维护性困难的问题。在.net平台下,可以使用nbusservice,enode规范项目的开发,提高项目的维护性。
第二个理由是,greenrobot eventbus 是可以提高不少的安卓性能。 这个是官方的统计。
Benchmark results indicate that EventBus is significantly faster in almost every scenario:
EventBus | Otto | |
---|---|---|
Posting 1000 events, Android 2.3 emulator | ~70% faster | |
Posting 1000 events, S3 Android 4.0 | ~110% faster | |
Register 1000 subscribers, Android 2.3 emulator | ~10% faster | |
Register 1000 subscribers, S3 Android 4.0 | ~70% faster | |
Register subscribers cold start, Android 2.3 emulator | ~350% faster | |
Register subscribers cold start, S3 Android 4.0 | About the same |
第三个理由是 greenrobot eventbus 可以大大简化android多线程,异步,前台线程与后台线程交互的复杂度。 简单程度基本跟 写一个“helloworld” 差不多了。
下面是官方的例子。
In EventBus, you may define the thread that will call the event handling method onEvent
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:
// Called in the same thread (default)
public void onEvent(MessageEvent event) {
log(event.message);
}
- MainThread: Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is the main thread, event handler methods will be called directly. Event handlers using this mode must return quickly to avoid blocking the main thread. Example:
// Called in Android UI's main thread
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.
// Called in the background thread
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.
// Called in a separate thread
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.).
配图说明 greenrobot eventbus 使用的简单程度 ,只有简单的三个步骤,已经简单得不能再简单了
Here we pick up on the 3 steps of the README and expand a bit on the code.
1: Define events
Events are POJO (plain old Java object) without any specific requirements.
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}
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.
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
// This method will be called when a MessageEvent is posted
public void onEvent(MessageEvent event){
Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}
// This method will be called when a SomeOtherEvent is posted
public void onEvent(SomeOtherEvent event){
doSomethingWith(event);
}
至此,介绍greenrobot eventbus结束,虽然基本都是官方的东西,希望你能在这里了解到android 下这个eventbus真的是好东西,能给你android项目带来的帮助,提高项目的维护性,一天轻轻松松完成工作,早点下班,珍惜生命。