# \[Pitch\] Transactional observation of values

**URL:** <https://forums.swift.org/t/pitch-transactional-observation-of-values/78315>\
**Category:** Pitches\
**Tags:** concurrency, asyncsequence, observation\
**Created:** [March 5, 2025, 9:07pm UTC](https://forums.swift.org/t/pitch-transactional-observation-of-values/78315 "2025-03-05T21:07:01Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![Matt\_Gallagher](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/matt_gallagher/32/2685_2.png) [@Matt\_Gallagher](https://forums.swift.org/u/Matt_Gallagher)\
**Post date:** [March 8, 2025, 5:02am UTC](https://forums.swift.org/t/pitch-transactional-observation-of-values/78315/19 "2025-03-08T05:02:18Z")

</div>

Hmm. I am seeing a problem where values in the sequence are not always emitted (it can be any non-initial value but it's most annoying when it's the last value).

I'm running the following code using the approach described in my previous comment. In the following code, `increment()` and `end()` are called back-to-back and the result seems to be non-deterministic about whether `Observed` will emit `0` then `nil`, (in which case the sequence will end as expected) or whether it will emit `0` then `1` and then the sequence will (unexpectedly) never complete.

It looks like `end()` may be getting called while the iterator is _between_ calls to `next()`... and in that case there's simply no observation at all? I think there needs to be a way to detect if a change has occurred between calls to `next()`, otherwise this isn't going to work reliably.

```swift
import Observation

actor SomeActor {
    @Observable
    class SomeObservable {
        var value: Int? = 0
    }
    var something = SomeObservable()
    func somethingSequence() -> Observed<Int, Never> {
        return Observed {
            print("Evaluating contents \(self.something.value == nil)")
            return self.something.value
        }
    }
    func increment() {
        print("Incremented")
        something.value = something.value.map { $0 + 1 }
    }
    
    func end() {
        print("Ending")
        something.value = nil
    }
}

let actor = SomeActor()

let task = Task {
    print("Start of task")
    let sequence = await actor.somethingSequence()
    for await value in sequence {
        print("The current value is \(value)")
    }
    print("End of task")
}

try? await Task.sleep(for: .seconds(1))
await actor.increment()
await actor.end()
try? await Task.sleep(for: .seconds(1))
await task.value

print("End of process")

```

---

_[View the full topic](https://forums.swift.org/t/pitch-transactional-observation-of-values/78315)._
