Friday, June 5, 2026
HomeiOS Developmentios - @Appstorage get caught/will not replace between views

ios – @Appstorage get caught/will not replace between views

[ad_1]

I am making my very first app in Swift with XCode 13.4.1, which is a Pomodoro. On this app, person can have as much as 4 duties

However I am dealing with to an issue and I am unable to discover a resolution and it appears like a bug :/

The var are set like this (that is not optimum I do know) :

   @AppStorage(wrappedValue: false, "task1") var task1
   @AppStorage(wrappedValue: "Nom", "task1Name") var task1Name
   @AppStorage(wrappedValue: 0, "task1TimeLeft") var task1TimeLeft
   @AppStorage(wrappedValue: 0, "task1Duration") var task1Duration
   // Similar for the three different job

When the person create a job, a pop-up view is present and person set a reputation, a period and the class. When that is achieved, person return to the view the place he can handle all of the duties.

When the person set the period of a job with the var “task_Duration” (_ = 1,2,3 or 4), the duty supervisor reveals the period with a progress bar and a label, the primary view solely do one factor : withdraw one seconds every seconds to the var.

I’ve 3 views linked to three courses : a primary view with timer, a view to handle job and a final one to create one.
To share the variable between the views, I exploit @AppStorage like this :

   class ViewController: UIViewController { 
   /// The principle view which reveals the timer

   @AppStorage(wrappedValue: false, "task1") var task1
   @AppStorage(wrappedValue: "Nom", "task1Name") var task1Name
   @AppStorage(wrappedValue: 0, "task1TimeLeft") var task1TimeLeft
   @AppStorage(wrappedValue: 0, "task1Duration") var task1Duration
   
   // I skip all of the pointless code

   func updateTimer(){ // a timer withdraw 1 to time var each seconds
       if time <1{
          // timer finish, if timeLeft == 0 the duty is deleted (task_ = false)
       } else{
          task_TimeLeft -= 1
       }
   }

  }

  class TaskManager: UIViewController {
   @AppStorage(wrappedValue: false, "task1") var task1
   @AppStorage(wrappedValue: "Nom", "task1Name") var task1Name
   @AppStorage(wrappedValue: 0, "task1TimeLeft") var task1TimeLeft
   @AppStorage(wrappedValue: 0, "task1Duration") var task1Duration
   ... (and the opposite)
   
   // this view present all of the informations concerning the job, nothing fascinating for the issue 
      besides that the "taskTimeLeft" is okay earlier than the primary view withdraw one
  }

  class TaskEditor: UIViewController {
    @AppStorage(wrappedValue: false, "task1") var task1
    @AppStorage(wrappedValue: "Nom", "task1Name") var task1Name
    @AppStorage(wrappedValue: 0, "task1TimeLeft") var task1TimeLeft
    @AppStorage(wrappedValue: 0, "task1Duration") var task1Duration
    ... (and the opposite)

    @IBAction func closeButtonTapped(_ sender: Any) {
       
       if task1IsEditing == true // a var outlined when person contact the duty within the supervisor {
           task1Name = String(format: "(taskTextField.textual content ?? "Sans nom")")
           task1 = true
           task1Duration = Int(taskTimeSlider.worth) * 60
           task1TimeLeft = task1Duration
           
           performSegue(withIdentifier: "managerFromTask", sender: self)
   }
  }

So, right here is the issue : when the person begin the timer with a job, all the things is okay, the taskTimeLeft is diminished. However, if I outline a brand new taskTimeLeft with the others views and if I begin the timer, the primary view is caught and withdraw 1 to the the final taskTimeLeft

For instance, I set a pomodoro with a 25 min period, I begin the timer however cease it 10 min later -> taskTimeLeft = 15min. I delete the duty and create one other one with a 40min period. Then I begin the timer, cease it 5 min later aand -> taskTimeLeft = 10 min ??

By the best way, I even have the very same downside when a button set a var named “taskSelected” to 0, the opposite views cannot change that 0, I must restart the app to alter the 0 the the worth that I set.

I attempted outdated strategies like UserDefaults.normal.actualize() nevertheless it would not work and depreciated.

Please are you able to assist me, I am caught on this downside since many days :'(

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments