Using RxSwift To Pass Data Between ViewControllers

Umair Ishrat Khan
4 min readDec 5, 2021

--

Most often protocols or delegates are used to pass data between ViewControllers. If you have red my previous articles then you are good to go with this one else I would recommend you to read my previous articles about RxSwfit. In my last article I talked about Subjects in RxSwift.

Today we are going to user Subject to pass image between ViewControllers.
Lets get start you can download the starter project from here.

Once you have downloaded the starter project. Navigate to the directory of this project using terminal just type “ pod install ” and hit enter. Make sure the project is closed when you do it. This will install the required pods.

Once you open the project you will see the following files. ViewController which has a imageView in it. ImagePickerViewController its our custom imagePickerView. ImageViewCell that is used for custom imagePickerController. I’ve setup the entire UI and also the imagePickerController to display images from photo library.

Next we will move to ImagePickerViewController. Here we will create a PublishSubjet which accept a UIImage but before doing that you need to import RxSwift.

import RxSwift

Then write the following code inside the class body.

private let selectedImageSubject = PublishSubject<UIImage>()

var selectedImage : Observable<UIImage> {

return selectedImageSubject.asObservable()

}

Observer & Observable

let move to forward and start using this subject.

Inside collectionView delegate didSelectItemAt we will write the following code:

let asset = assetImage[indexPath.row]

PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width: 150, height: 150), contentMode: .aspectFill, options: .none) { [weak self] image, info in

guard let info = info else {return}

//Check if its a thumnail image

let thumbnailImage = info[“PHImageResultIsDegradedKey”] as! Bool

// If not Thumbnail Image

if !thumbnailImage {

//Observe the image with Subject

self?.selectedImageSubject.onNext(image!)

}

}

asset here is the array that holds all the assets we fetched from user’s photo library. We are using PHImageManager to convert the PHasset to UIImage.

Where as the thumbnailImage is checking if the image that we are converting is a thumnailImage or real image if its not thumbnailImage then we proceed further & observe this image with our selectedImageSubject that we created earlier.

Since we have observed the image lets move to the ViewController class and see how can the subject now act as observable and display the image to our ViewContoller’s imageView.

We will call subscribe to the observable inside our rightbarbutton item action function.

RxSwift

As soon as we tap to the + button we want to present out imagePicker Controller and subscribe to our observable in order to listen the events.

When we tap the + button it will present the ImagepickerController. Once we pick the image our Viewcontroller will display it.

Now as we understood the flow lets see the implementation.

Inside our starter project you will find this code except for the code from line 66–70. So what actually is this code doing?. line 66 we are subscribing and onNext is providing us the value of next event i.e image so when we select a new image that value is passed here and gets assigned to our view controller imageView on line 68.

Finally on line 70 we are deallocating the resources that was used to compute the subscriber.

Hopefully I was able to explain you well if you feel the same kindly follow me. I’ll be posting more articles on Swift. You can download the final project here.

--

--

Umair Ishrat Khan

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.