Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DonKitのリファクタリングをしたい #17

Open
shibafu528 opened this issue Jun 13, 2021 · 2 comments
Open

DonKitのリファクタリングをしたい #17

shibafu528 opened this issue Jun 13, 2021 · 2 comments

Comments

@shibafu528
Copy link
Owner

shibafu528 commented Jun 13, 2021

DonKitのHTTPリクエストを伴うメソッドはだいたい success:failure: で終わるようになっている。これは、AFNetworkingのシグネチャを模したものである。
ただ、この形式のコールバックは微妙に扱いにくい気がしている…… リファクタリングを検討したい。

Pattern 1 : PromiseKitを使い、Promiseベースに置き換える

いちおう本命のパターン。

PromiseKitを既に導入しているため、戻り値をAnyPromiseにしてコールバックと繋ぎこめば .then() とか書けるようになる。 既にPromiseで処理したくてその場でラッピングのコードを書いている場所がいくつかあり、悪くないと思う。

欠点があるとすれば .then で渡ってくる結果の型は id なことか。呼び出しの度に実際に渡る型を調べて書く必要がある。

Pattern 2 : Apple式の completionHandler: に集約する

コールバックを2つ書くのと、1つ書いて if (error) するのとどっちがマシかみたいな美的感覚の問題が何割かありそうだと思ってのパターン。
(failure: が独立して渡せることで、エラー処理をまとめることができるというのはあるんだが……)

最後のコールバックが1つだとだいぶ呼び出しの見かけがマシになる。現状の failure: 側の処理は雑なのでそんなに書き換えで残念なことになりそうな場所もない。

ちなみにこちらを採用すると、DonKitを独立させた場合にSwift 5.5からは勝手にasync関数として使えるようになるらしいぞ。

@shibafu528 shibafu528 changed the title DonKitのリファクタリングをする DonKitのリファクタリングをしたい Jun 13, 2021
@shibafu528
Copy link
Owner Author

Effective Objective-C 2.0では後者の実装パターンが推されていた。なるほど。

@shibafu528
Copy link
Owner Author

新規に書くコードではPattern 2を採用してみる。Promiseのほうが多分書きやすいけど、idは辛いかな…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant