よくある Observable の入れ子ができない頭の硬さ乙

99

なぜか長ったらしくなる Observable のネスト.


Observable.just("yo")
    .flatMap(s -> Observable.range(0, 100))
    .subscribe(integer -> Ln.d("Here's an Integer(%s), but how do I access that juicy String?", integer));

Passing composite data through flatMap and similar operators without creating container objects · Issue #2931 · ReactiveX/RxJava

よくあるくせに綺麗に書けない.

そんなわたし乙.

lambda も絡めてゆっくりと眺める.

元.


Observable.just("yo")
    .flatMap(new Func1<String, Observable<? extends Integer>>() {
      @Override public Observable<? extends Integer> call(String s) {
        return Observable.range(0, 100);
      }
    })
    .map(new Func1<Integer, String>() {
      @Override public String call(Integer integer) {
        return String.format("Here's an Integer(%s), but how do I access that juicy String?", integer);
      }
    })
    .subscribe(new Action1<String>() {
      @Override public void call(String x) {
        System.out.println(x);
      }
    });

flatMap(Func1, Func2) でやる場合.


Observable.just("yo")
    .flatMap(new Func1<String, Observable<? extends Integer>>() {
      @Override public Observable<? extends Integer> call(String s) {
        return Observable.range(0, 100);
      }
    }, new Func2<String, Integer, String>() {
      @Override public String call(String s, Integer integer) {
        return String.format("Here's an Integer(%s), with String(%s)", integer, s);
      }
    })
    .subscribe(new Action1<String>() {
      @Override public void call(String x) {
        System.out.println(x);
      }
    });

flatmap に map を入れ子でやる場合.


Observable.just("yo")
    .flatMap(new Func1<String, Observable<? extends String>>() {
      @Override public Observable<? extends String> call(String s) {
        return Observable.range(0, 100)
            .map(new Func1<Integer, String>() {
              @Override public String call(Integer integer) {
                return String.format("Here's an Integer(%s), with String(%s)", integer, s);
              }
            });
      }
    })
    .subscribe(new Action1<String>() {
      @Override public void call(String x) {
        System.out.println(x);
      }
    });
  }

lambda へ

元.


Observable.just("yo")
    .flatMap(s -> Observable.range(0, 100))
    .map(integer -> String.format("Here's an Integer(%s), but how do I access that juicy String?", integer))
    .subscribe(System.out::println);

flatMap(Func1, Func2) でやる場合.


Observable.just("yo")
    .flatMap(s -> Observable.range(0, 100),
        (s, integer) -> String.format("Here's an Integer(%s), with String(%s)", integer, s))
    .subscribe(System.out::println);

flatmap に map を入れ子でやる場合.


Observable.just("yo")
    .flatMap(s -> Observable.range(0, 100)
        .map(integer -> String.format("Here's an Integer(%s), with String(%s)", integer, s)))
    .subscribe(System.out::println);

まとめ

flatmap に map を入れるのがわかりやすいように思います.

慣れたらラムダも直感的です.

Feature request: @Passthru variable for Rx chains · Issue #855 · square/retrofit


意味不明な Gradle の Warning や Error を消して激ビルド時間短縮

なんとなく Gradle コンソールを開けてみると死ぬほど書き出されてるエラーやワーニング.


AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$8) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$9) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$1) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from s

いつから出ていたのかわかりません.

Proguard 周りなのでしょうが, 調べても意味がわかりませんでした.

が,

Large number of errors during Gradle build after upgrading to Android Studio 2.0 - Stack Overflow

意味のない warning や error は, .idea フォルダを消すことで取り除くことができます。このフォルダは Android Studio 1.5 が作成したもので 2.0 にインポートされています。消しても自動的に作成されます。

ideadir

一度削除すると再作成されますが, 意味不明なログが吐かれなくなり, ビルド時間も数分から数十秒に激速となりました。

...

などと喜んでいたが, また出てる.

...

Issue Tacker を見てみる.

Issue 215748 - android - AGPBI reports errors instead of warnings - Android Open Source Project - Issue Tracker - Google Project Hosting

まだ生きてる issue なのですが, 一時しのぎとして以下とか.


# https://code.google.com/p/android/issues/detail?id=215748
-keepattributes Exceptions, Signature, InnerClasses, EnclosingMethod
-dontoptimize

一応出なくなりますけれども,

速くなりますけれども,

「don't optimize」て...

またはこんな一時しのぎ法も.

Issue 222989 - android - Android Studio 2.2 : Build successful with 1944 errors - Android Open Source Project - Issue Tracker - Google Project Hosting

It affects me too it is caused by gradle not android studio (but it tries to update the gradle)
Temporary solution:
I went back to gradle 2.1.3 (that I used earlier):

classpath 'com.android.tools.build:gradle:2.1.3' ->this works

If I am using classpath 'com.android.tools.build:gradle:2.2.0' I am also getting tons of warning message and the output is an unaligned file...

「com.android.tools.build:gradle のバージョンを下げる」という方法ですが, うまくダウングレードできないような...

なんなんすかね, まったく.

issues_-_android_-_android_open_source_project_-_issue_tracker_-_google_project_hosting

Issues - android - Android Open Source Project - Issue Tracker - Google Project Hosting

2016-10-05

待てないので以下でとりあえず対応.
AGPBI 関連の多量のエラーを排除する


Dependency Injection 一歩目

3173827605_427626c6af-1

話題でした.

「なぜDI(依存性注入)が必要なのか?」についてGoogleが解説しているページを翻訳した  - Qiita

これを幼稚に理解しておこうかとな.

 

「Dependency Injection」という言葉

「依存性注入」てのがしっくりこないので少し調べてみる.

「オブジェクトを注入する」と言うと日本語としても意味がはっきりしていますね。

「Dependency」は「依存性」ではなく「使われる側のオブジェクト」(依存オブジェクト)という意味になります。

やはりあなた方のDependency Injectionはまちがっている。 — A Day in Serenity

「外部オブジェクトの注入」と覚えたら直感的にわかりやすいですよね.

 

修正前/後のコードを比べてみる

修正前.

メソッド内で外部オブジェクトを生成している.


public class RealBillingService implements BillingService { 

  public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
    CreditCardProcessor processor = new PaypalCreditCardProcessor();
    TransactionLog transactionLog = new DatabaseTransactionLog();
    // ...
  }

}

修正後.

外部オブジェクトを外部で生成してコンストラクタから注入している.


public class RealBillingService implements BillingService {

  private final CreditCardProcessor processor;
  private final TransactionLog transactionLog;

  public RealBillingService(CreditCardProcessor processor,TransactionLog transactionLog) {
    this.processor = processor;
    this.transactionLog = transactionLog;
  }

  public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
     // ...
  }
}

これをテスト時には, メンバ初期化で生成したオブジェクトを各メソッド内でコンストラクタから注入して利用する.


public class RealBillingServiceTest extends TestCase {

  private final PizzaOrder order = new PizzaOrder(100);
  private final CreditCard creditCard = new CreditCard("1234", 11, 2010);

  private final InMemoryTransactionLog transactionLog = new InMemoryTransactionLog();
  private final FakeCreditCardProcessor processor = new FakeCreditCardProcessor();

  public void testSuccessfulCharge() {
    RealBillingService billingService
        = new RealBillingService(processor, transactionLog);
    Receipt receipt = billingService.chargeOrder(order, creditCard);

    // assert etc...
  }

}

 

例) データ抽出先のサーバを切り替える

Repository クラスに, RemoteDataSource オブジェクトを注入しておいてテスト時には切り替えます.

それぞれのインスタンスは Singleton によって一意であることを保証しています.

公開用コード.
android-architecture/Injection.java at todo-mvp · googlesamples/android-architecture


public class Injection {

    public static TasksRepository provideTasksRepository(@NonNull Context context) {
        checkNotNull(context);
        return TasksRepository.getInstance(TasksRemoteDataSource.getInstance(),
                TasksLocalDataSource.getInstance(context));
    }
}

テスト用コード.
android-architecture/Injection.java at todo-mvp · googlesamples/android-architecture


public class Injection {

    public static TasksRepository provideTasksRepository(@NonNull Context context) {
        checkNotNull(context);
        return TasksRepository.getInstance(FakeTasksRemoteDataSource.getInstance(),
                TasksLocalDataSource.getInstance(context));
    }
}

外部オブジェクトの変更が容易になっていることがよくわかります.

注入の方法は他にもあるようですがまあいいか.

 

まとめ

メソッド外で生成してコンストラクタから注入する.
(メソッド内で生成しない)