UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 47 Challenge Issue- Milestone project 4

Forums > 100 Days of SwiftUI

I am working on Habbit tracker project..everything is going fine and able to complete the challenge. But there is one issue I am facing is:

I try to edit the activity item details but it is not rendering in Main ContentView after dismissing the edit controller.

I debugged my code and cross checked that the activity is also replaced in the main array and saving the updated value in UserDefaults as well, but only not rendering the updated array in main view. If I run the app again then it shows the correct value.

If I add new activity then it works just fine..and renders the correct data in main contentview

So I am wondering why its not working only in Edit mode?

Any help would greatly be appreciated!

2      

Hi @iRiziya, look at other topic for Day 47 (i have just answerd to) in the forum you have 2 finished code examples wich can help you.

3      

Hi @linkiliz,

Thanks for the response!

Yes I checked both the finished code examples and I found your code something similar to mine. The one thing I missed was @State for the activity to be edited in the next screen. I changed to it but still its not working!

Here is my code: https://we.tl/t-pPF3XfiiXi

Can you please check once where I went wrong?

I am using Xcode 11.3

Thanks!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Can not see you code without signing up. Try to put on github (as everyone who use this forum has). You might get better response.

2      

Hi, You should debug here (on the save action) :

if let activity = self.activityToEdit
            {
                guard let index = self.habbit.activities.firstIndex(where: { $0.id == activity.id }) else { return }

                var arrActivities:[Activity] = self.habbit.activities

                let activityToReplace = Activity(title: self.title, description: self.description, completionCount: self.counter)

                arrActivities[index] = activityToReplace

//                self.habbit.activities[index].replace(activity: activityToReplace)

                self.habbit.activities = arrActivities
            }

For the same result, i only do this :

func updateActivity() {
        if let position = allActivities.items.firstIndex(where: { $0.id == activity.id }) {
            allActivities.items[position] = activity
        }
    }

The change of your array seems not be trigger (also check that it updates properly), I would dig there if I were you.

3      

Hi @linkiliz,

I have tried the given lines of code but its not working!

In debug mode, it prints the old array and showing not updated. Then I do nothing and run the app again, now it shows the updated array, that means the array is actually being updated but not rendering correctly.

2      

my code was not used for you to copy it, just to show you a simpler method to update your array that i used. Whatever, I took a look, I just haded a simple print to your func SaveButton () in the if let activity = self.activityToEdit condition, and it doesn't trigger. Just find why, that's why your array is not updated.

2      

I tried to print inside all if blocks and it does print everytime. (Wondering why it didn't trigger for you!!)

Like I said earlier, it also gives the correct result while debugging! If my code is not supposed to update the array then from where the UserDefaults is fetching the correct array when I run the app again.

I doubt it is the Xcode version!

2      

Replace your code by this :

if let activity = self.activityToEdit
            {
                print("EDIT ACTIVITY.............")
                print(self.habbit.activities)
                print("hmmmm.... empty array")

                if let position = self.habbit.activities.firstIndex(where: { $0.id == activity.id }) {
                    self.habbit.activities[position] = activity
                }

                print(self.habbit.activities)
            }

(for me) Your array is empty from the start of this function.

If for you the result is not the same and the array is not empty at this point, then it is maybe because of the version that we don't have same thing (the didSet broken in 11.4). But i think your code is supposed to work for me even if it is the case, but it doesn´t.

2      

Yeah it should be the version issue probably as neither the array is empty for me nor of anthing you are saying.

Thank you for the response. I will have 11.4 soon.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.