Thread
#reaktive
    saket

    saket

    1 year ago
    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:
    struct FooView: View {
      let presenter: FooPresenter
    
      var body: some View {
        Present(presenter) { model ->
          Text(model.name)
        }
      }
    }
    Arkadii Ivanov

    Arkadii Ivanov

    1 year ago
    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.
    saket

    saket

    1 year ago
    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.
    Arkadii Ivanov

    Arkadii Ivanov

    1 year ago
    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

    1 year ago
    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.
    saket

    saket

    1 year ago
    @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

    1 year ago
    @saket my swift skills are not good too.
    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
    saket

    saket

    1 year ago
    thank you @lehakorshun, I’ll try this out!