I want to design my swift app using clean architecture.
I have read the link below on clean architecture for swift
http://clean-swift.com/blog/
I have used these templates to create the app.
I wanted to ask if this is the best architecture to use as I want to code using Rx-Swift.
Also I would appreciate if someone could point out a few working examples for clean architecture with reactive swift.
Is using Uncle Bob’s Clean Architecture for swift(http://clean-swift.com/blog/) a best practise for RxSwift or should I go with MVVM architecture
Any help will be appreciated. Thank you.
The Clean Swift architecture is using a VIP cycle to help you separate logic in your application. The VIP cycle consists of a ViewController, Interactor and a Presenter. All classes are responsible for some logic.
Start using RxSwift with MVVM pattern At first, we need to add RxSwift to the project. In this example, we'll use CocoaPods but you can also use Carthage and Swift Package Manager. Check the GitHub repo for more info. RxSwift adds the basic library including Observable, Variable, PublishSubject etc.
Clean architecture allows us to create architectural boundaries between dependencies which allows components to be intrinsically testable. What's to follow is our attempt to build an iOS application using clean architecture with TDD. The app will be used to manage contacts.
Hi I'm using rxswift with clean architecture recently project
In my case, I use them in Profile scene especially looks like instagram
use case of profile scene
problem is, user can change post's category rapidly then
last fetched post's should be different to selected category :(
all event cause event asyncronousely
so I use rxswift
In Interactor has profileWorker(to fetch profile and category's post count), postWorke(to fetch posts)
and create three rxswift PublishSubject
like
- rxCategory
- rxProfile
- rxPosts
Step is like below
Interactor
subscribe rxCategory
ViewController
request category change to Interactor
Interactor
cause event rxCategory.on(Event<Category>.next(category))
Interactor
using rxswift's flatMapLatest request posts with selected Category to postsWorker
rxCategory.asObservable().flatMapLatest{ (category) -> Observable<[Posts]> in
return self.postsWorker.rxFetchPosts(requestModel, category: category)
}.subscribe { (event) in
if let posts = event.element {
self.updatePosts(posts)
}
}
}
PostsWorker
request Observable<[Posts]>
to PostService (request to server using Alamofire
and SwiftyJSON
)
then PostService
return Observable<Posts>
then worker return to interactor
Interactor
subscribe PostsWorker
's Observable and update posts , this cause rxPosts
s event accur
Interactor
subscribe rxProfile
and rxPosts
using combineLatest
and request presenting combined response to Presentor
Observable.combineLatest(rxProfile, rxPosts) { (e1, e2) -> ProfileResponse in
return ProfileResponse(profile: e1, posts: e2)
}.subscribe { (event: Event<ProfileResponse>) in
self.output.presentProfile(event.element!)
}
}
this is my example using rxswift + clean swift architecture
in this situation, if user tap FOOD
category and worker fetch many posts
and user tap PLACE
category and fetch small posts then sometimes
although user tap people category. food posts fetched after people's posts
so I use rxswift to match correctly to selected category using flatMapLatest ReativeX - FlatMapLatest
this can fetch and present to Presentor
latest category's posts
rxswift known useful in MVVM
architecture.
but I think it's up to you how can use them
thank you for reading :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With