Dimillian / Dimillian/IceCubesApp

Bug/Discussion: ViewModels are initialised multiple times?

Open
#2,033 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Swift
Stars
7.1k
Forks
720
PR merge metrics
No merged PRs in 30d

Description

I'm not sure if this is a bug tbh but I've noticed that @Observable objects are being initialised multiple times.
So this @State private var viewModel = TimelineViewModel() for example would call TimelineViewModel.init multiple times.

Here is a forum post I could find about this "issue" https://forums.swift.org/t/observable-init-called-multiple-times-by-state-different-behavior-to-stateobject/70811/8
Apple also suggests initialising objects inside .task which would leave us with optionals everywhere...
https://developer.apple.com/documentation/swiftui/state#Store-observable-objects

Contributor guide

No contributing guide indexed for this repository

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 with the @State private var viewModel = TimelineViewModel() pattern and reproduce the repeated initialization behavior using the linked Swift forum discussion and Apple State documentation. Determine whether the behavior is expected and what concrete change or guidance is warranted; done means the initialization lifecycle and resolution are clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, swift
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.