What's the correct way to consume a Kotlin flow vi...
# touchlab-tools
d
What's the correct way to consume a Kotlin flow via https://skie.touchlab.co/features/flows when in UIKit world? i.e. how would I do something like this? (assuming everything else is like the example in the docs)
Copy code
override func viewDidLoad() {
  for await it in flow {
    // No type cast (eg `it as! KotlinInt`) is needed because the generic type is preserved.
    let number: KotlinInt = it
    print(number)
  }
}
đź‘Ś 1
s
Yes, but it must be called in an async context (Task).
d
yep, was curious if there's a "correct" way to do this? I can see
Task.runDetached
exists but the skie docs don't mention it or anything else. Didn't want to break some internal contract by running it detached
so is the correct way this then?
Copy code
override func viewDidLoad() {
  ...
  Task.runDetached {
    for await it in flow {
      // No type cast (eg `it as! KotlinInt`) is needed because the generic type is preserved.
      let number: KotlinInt = it
      print(number)
    }
  }
}
s
I think, but i'm not sure, using
Task { ..... }
should suffice, no need to use
.detached
(
.runDetached
is deprecated, IIRC).
âž• 1
f
Hi! SKIE converts Flows to AsyncSequences in a way that should be fully compatible with anything that you can do with AsyncSequences in Swift. That means that you shouldn’t be able to break it by doing anything that is supposed to work with regular AsyncSequences (including running it in background threads). The same goes for how to use Flows in UIKit - just follow some guide for using AsnycSequences. For example this article appears to have some examples of that: https://dimillian.medium.com/use-task-with-asyncstream-and-avoid-retain-cycle-4c92dddf30ef
thank you color 1