Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Subscribing to changes with for await not working correctly? #165

Answered by trispo
trispo asked this question in Q&A
Discussion options

Hey @ALL,

I've a in-memory shared user in my app.

When I subscribe to changes within a Task to it I do not receive the changes:

Task {
 for await user in $user.publisher.values {
 print(user)
 }
}

This prints only the initial value but not the changes. The task never finishes nor does it get cancelled as far as I know.

When I subscribe to the changes via Combine it works:

cancellable = $user.publisher.sink { user in
 print(user)
}

What am I doing wrong?

You must be logged in to vote

Hi @ALL. It was my own dumbness. I had a guard statement within the for await loop and in its else block I had a return statement which canceled the loop. 🫣

Replies: 1 comment 7 replies

Comment options

In-memory keys don't implement any kind of subscribing whatsoever currently: https://github.com/pointfreeco/swift-sharing/blob/main/Sources/Sharing/SharedKeys/InMemoryKey.swift#L56-L60

I suppose it would be a welcome addition to the library in a PR?

You must be logged in to vote
7 replies
Comment options

Wild guess, have you checked you don't accidentally overwrite _user (mind the underscore, as opposed to $user) with another Shared<...>? That appears to be a trap for some.

Comment options

No, I don't

Comment options

@pyrtsa that subscription is left empty because there is no external storage system that needs to be subscribed to. But that shouldn’t affect how the publisher works.

Comment options

Hi @ALL. It was my own dumbness. I had a guard statement within the for await loop and in its else block I had a return statement which canceled the loop. 🫣

Answer selected by trispo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /