skiptools / skiptools/skip-ui

Possible issue with modifying @State variables within .task?

Open
#30 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
330
Forks
76
Avg merge
3h 6m
Merged PRs (30d)
1

Description

I ran across a weird issue wherein setting a @State variable from within .task based on an array of values (I tested with UUIDs and random Ints) works properly in SwiftUI but not Compose.

In a nutshell, assume that a view struct has the following variables:

let uuids = [UUID(), UUID(), UUID(), UUID(), UUID()]
@State private var firstUUID:UUID?

This initializes the view with 5 random UUIDs and a firstUUID @State variable set to nil.

Within the view's .task, I attempt to set firstUUID to the first UUID in the array.

.task {
    self.firstUUID	= self.uuids.first
}

This works properly within SwiftUI, but not Compose.

(Real-world context: I want to use UUIDs to model different pieces of model that a user can toggle between in the view...)

I do note, however, that (assuming these views are in tabs) if I click on a different tab and then return the value is set properly in Compose as well. It also seems to work consistently within the second tab. Some sort of timing issue, perhaps?

I tried this using random Ints and saw the same behavior with them, so I don't think it's specific to UUID, per se.

This has been a bit hard to explain, so I've created a test project that I believe demonstrates what I'm seeing. I will attach a screenshot of the output from Compose (left) and SwiftUI (right), respectively.

You'll see that Compose consistently identifies the first UUID in the array within view reconstruction, but comes up with a completely different UUID when it's based on the @State variable.

Screenshot 2024-05-02 at 3 06 20 AM

This might seem a bit esoteric, but I found it while having trouble getting a struct to conform to Identifiable (hence the UUID - I was using one for my struct's id). I imagine others will run across this as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked skip-uuidtest project and compare the .task assignment to the @State value during view reconstruction and tab switching. Trace the Compose-side handling of @State and .task to identify why the first UUID differs from the state-backed value. Done means the state assignment behaves consistently with SwiftUI, with regression coverage for UUIDs or random integers.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, swift
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.