A.S A.S - 1 month ago 42
iOS Question

Rx-Swift Clean architecture

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.

Answer

Hi I'm using rxswift with clean architecture recently project

In my case, I use them in Profile scene especially looks like instagram

enter image description here

use case of profile scene

  • user should refresh profile scene
  • user should chance photo's category
  • user should load more photo's like pagination

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 rxPostss 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 :)