Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rx-Swift Clean architecture [closed]

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.

like image 931
A.S Avatar asked Oct 26 '16 10:10

A.S


People also ask

What is clean swift architecture?

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.

How do I use RxSwift with MVVM?

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.

What is clean architecture in iOS?

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.


1 Answers

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

like image 182
Cruz Avatar answered Oct 08 '22 12:10

Cruz