https://kotlinlang.org logo
#touchlab-tools
Title
# touchlab-tools
d

dorche

12/13/2023, 3:10 PM
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

streetsofboston

12/13/2023, 3:14 PM
Yes, but it must be called in an async context (Task).
d

dorche

12/13/2023, 3:18 PM
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

streetsofboston

12/13/2023, 3:25 PM
I think, but i'm not sure, using
Task { ..... }
should suffice, no need to use
.detached
(
.runDetached
is deprecated, IIRC).
1
f

Filip Dolník

12/13/2023, 3:46 PM
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
4 Views