In RxSwift, we should be careful while passing functions to operators which might cause a memory leak.

In this example, our main actor is RxSwiftTest class. It creates an observable in init and passes multiplyByTwo function to map operator to transform the result. Passing the function causes observable to…

--

--

I will show the example first:

In this example, we have an Observable emitting events every 300 milliseconds and a Disposable that is returning as the result of subscribe function.

Here, although our view controller does not keep a strong reference for this subscription, the console keeps printing events:

--

--

Let’s say we have 2 business logics and one needs the other ones’ output to process. Here in this case we can get help from Future in Combine:

The business logic functions returns Future, we tie them up with flatMap and then subscribe with sink function. See that we erased the Future type to AnyPublisher using eraseToAnyPublisher to make it more flexible.

Now, the business logics might return error, in that case we can handle it in many ways with combine. One way of handling would be:

Here, failingBusinessLogic function returns failure and when we subscribe to it, we get completion with error.

--

--

One difference between them is CurrentValueSubject has an initial value and emits the latest value to new subscribers. I will show with an example. First, the PassthroughSubject:

As you see, subscription2 does not get any value since PassthroughSubject does not emit latest value. Now, the CurrentValueSubject:

See that…

--

--

If we want to load the content into memory when we need it, we can use LazyHStack. It is particularly useful when we have large list of contents inside a scrollview.

Here’s how our regular HStack looks like:

HStack has 1000 Text content in it and the background color of HStack is blue:

--

--

If we want to load the content into memory when we need it, we can use LazyVStack. It is particularly useful when we have large list of contents inside a scrollview.

Here’s how our regular VStack looks like:

VStack has 1000 Text content in it and the background color of VStack is blue:

--

--

In SwiftUI, shapes are built using paths. To create a custom shape we need to conform to Shape protocol and do the drawing inside path(in:) function. The parameter function takes is a rectangle and it is the reference to the frame that the shape is going to reside. Let’s draw a Cross:

And add it into a VStack to test it out:

Here’s the result:

--

--