https://kotlinlang.org logo
#reaktive
Title
# reaktive
s

saket

08/13/2021, 2:50 PM
I found a super simple way for using Reaktive streams with SwiftUI. Sharing it here in case it’s useful to others: https://gist.github.com/saket/62b1ccfc1a92c393be60a1cfd5357d98 Here’s what my usage code looks like:
Copy code
struct FooView: View {
  let presenter: FooPresenter

  var body: some View {
    Present(presenter) { model ->
      Text(model.name)
    }
  }
}
a

Arkadii Ivanov

08/13/2021, 3:01 PM
Nice! Thanks for sharing! Please mind reference cycles, because Swift don't collect them. I suspect your Present view references the presenter, which in turn references the view on subscription. Maybe
weak
closure will help.
Unlike coroutines, Reaktive can be consumed from Swift with much less workarounds.
s

saket

08/13/2021, 5:48 PM
TIL about reference cycles. Do you know if it’s possible to verify if the cycle needs to be broken manually? I tried using weak self in the closure, but Swift says it’s not allowed for structs.
a

Arkadii Ivanov

08/13/2021, 5:52 PM
I'm not good at Swift, I heard about a memory profiler. It should be Googlable I think. From what I know, cyclic hard references are not collected at all.
l

lehakorshun

08/14/2021, 9:39 AM
The main issue of leak here is you are trying to access the Struct property inside the completion block which will basically create a new copy of your structure on assignment. You can try to create
weak let model
before call completion block, and use it inside.
s

saket

08/14/2021, 10:41 PM
@lehakorshun can I ask you to show me what this code would look like? My Swift skills are new and I’m not sure how to create a weak copy of model before using it inside `onReceive`’s closure.
l

lehakorshun

08/16/2021, 4:17 AM
@saket my swift skills are not good too.
Copy code
struct FooView: View {
  let presenter: FooPresenter

  var body: some View {
    weak let weakPresenter
    Present(weakPresent) { model ->
      Text(model.name)
    }
  }
}
I think it should be work, but I didn’t test it
s

saket

08/21/2021, 1:31 AM
thank you @lehakorshun, I’ll try this out!
7 Views