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 keep a strong reference to RxSwiftTest instance and RxSwiftTest can not get deallocated and subscription keeps living and printing the result:
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:
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, we have to initialize CurrentValueSubject with a value and subscription1 receives this initial value which was the latest by the time subscription1 was initialized. Also, subscription2 receives the latest value when initialized unlike the one in PassthroughSubject example.
Another difference is that you can get the current value of CurrentValueSubject by using its value property:
Also, you can set the value of CurrentValueSubject by using its value property again. (This is equivalent to send function which can be used for both CurrentValueSubject and PassthroughSubject to set the value)
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:
We can get system-wide settings like color scheme(light or dark), size class, device locale using @Environment property wrapper. The full list for the values/settings we can access are here: https://developer.apple.com/documentation/swiftui/environmentvalues
Let’s see an example:
Here, we have two environment values: colorScheme and verticalSizeClass. We change text color according to colorScheme and we switch between VStack and HStack according to verticalSizeClass.