sychaos / -READ-ObservableScheduler

更好的管理线程间跳转的库

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ObservableScheduler

Download

你的app是不是经常做一些很耗费是工作,但这些工作都是在主线程完成的,因为开线程然后回调主线程很麻烦的说。当然,我们可以用RxJava轻松实现, 但是如果你的项目没有集成RxJava怎么办,集成进去? 如果集成RxJava只为做这些事岂不是太浪费了。你最好集成RxAndroid,RxBus,RxLifecycle,RxBinding 是不是感觉要改架构了😂

ObservableScheduler 最主要的目的就是轻松的帮你在子线程和主线程之间做转换, 项目的**来自RxJava,代码很有可能也是模仿RxJava,没关系,人家牛我们就得模仿。

##源码分析

  • schedule包中的类 MainScheduler 利用主线程的Handler在主线程运行runnable方法 execute为无返回值的调用,submit为有返回值的调用 对应的是ExecutorService的execute和submit WorkScheduler 通过ExecutorService线程池运行方法
  • SubscribeManager 包装了实现Observer接口的类,对Observer的各种回调进行进一步包装
  • JObservable 其实没什么好讲的比较简单

##Demo:

##示例代码:

JObservable.create(new JObservable.OnSubscribe<List<Bitmap>>() {
            @Override
            public void call(SubscribeManager<List<Bitmap>> mSubscriber) {
                try {
                    //TODO... 
                    mSubscriber.notifyData(bitmaps);
                } catch (Exception e) {
                    mSubscriber.error(e);
                }
            }
        }).workedOn(Schedules.background())
                .subscribeOn(Schedules.mainThread())
                .subscribe(new Subscriber<List<Bitmap>>() {
                    @Override
                    public void notifyData(List<Bitmap> strings) {
                      //TODO... 
                    }
                    @Override
                    public void error(Throwable t) {
                      //TODO... 
                    }
                });

##用法: ###第一步: gradle:

compile 'com.jiang.android.observablescheduler:schedule:1.0.1'

Maven:

<dependency>
  <groupId>com.jiang.android.observablescheduler</groupId>
  <artifactId>schedule</artifactId>
  <version>1.0.1</version>
  <type>pom</type>
</dependency>

###第二步:

License

Copyright 2016 NewTab

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

更好的管理线程间跳转的库

License:Apache License 2.0


Languages

Language:Java 100.0%