|
36 | 36 | }
|
37 | 37 |
|
38 | 38 | #if DEBUG
|
39 |
| - @MainActor |
40 |
| - func testNestedObservation() async throws { |
41 |
| - XCTExpectFailure { |
42 |
| - $0.compactDescription == """ |
43 |
| - An "observe" was called from another "observe" closure, which can lead to \ |
44 |
| - over-observation and unintended side effects. |
| 39 | + @MainActor |
| 40 | + func testNestedObservation() async throws { |
| 41 | + XCTExpectFailure { |
| 42 | + $0.compactDescription == """ |
| 43 | + An "observe" was called from another "observe" closure, which can lead to \ |
| 44 | + over-observation and unintended side effects. |
45 | 45 |
|
46 |
| - Avoid nested closures by moving child observation into their own lifecycle methods. |
47 |
| - """ |
48 |
| - } |
| 46 | + Avoid nested closures by moving child observation into their own lifecycle methods. |
| 47 | + """ |
| 48 | + } |
49 | 49 |
|
50 |
| - let model = Model() |
51 |
| - var counts: [Int] = [] |
52 |
| - var innerObservation: Any! |
53 |
| - let observation = observe { [weak self] in |
54 |
| - guard let self else { return } |
55 |
| - counts.append(model.count) |
56 |
| - innerObservation = observe { |
57 |
| - _ = model.otherCount |
| 50 | + let model = Model() |
| 51 | + var counts: [Int] = [] |
| 52 | + var innerObservation: Any! |
| 53 | + let observation = observe { [weak self] in |
| 54 | + guard let self else { return } |
| 55 | + counts.append(model.count) |
| 56 | + innerObservation = observe { |
| 57 | + _ = model.otherCount |
| 58 | + } |
| 59 | + } |
| 60 | + defer { |
| 61 | + _ = observation |
| 62 | + _ = innerObservation |
58 | 63 | }
|
59 |
| - } |
60 |
| - defer { |
61 |
| - _ = observation |
62 |
| - _ = innerObservation |
63 |
| - } |
64 | 64 |
|
65 |
| - XCTAssertEqual(counts, [0]) |
| 65 | + XCTAssertEqual(counts, [0]) |
66 | 66 |
|
67 |
| - model.count += 1 |
68 |
| - try await Task.sleep(nanoseconds: 1_000_000) |
69 |
| - XCTAssertEqual(counts, [0, 1]) |
| 67 | + model.count += 1 |
| 68 | + try await Task.sleep(nanoseconds: 1_000_000) |
| 69 | + XCTAssertEqual(counts, [0, 1]) |
70 | 70 |
|
71 |
| - model.otherCount += 1 |
72 |
| - try await Task.sleep(nanoseconds: 1_000_000) |
73 |
| - XCTAssertEqual(counts, [0, 1, 1]) |
74 |
| - } |
| 71 | + model.otherCount += 1 |
| 72 | + try await Task.sleep(nanoseconds: 1_000_000) |
| 73 | + XCTAssertEqual(counts, [0, 1, 1]) |
| 74 | + } |
75 | 75 | #endif
|
76 | 76 | }
|
77 | 77 |
|
|
0 commit comments