# Your Second iOS and SwiftUI App · Observable Objects | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357>\
**Category:** 🎓 Courses & Articles\
**Created:** [October 9, 2019, 4:11am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357 "2019-10-09T04:11:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [October 9, 2019, 4:11am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/1 "2019-10-09T04:11:27Z")

</div>

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/5662524-your-second-ios-and-swiftui-app/lessons/13](https://www.raywenderlich.com/5662524-your-second-ios-and-swiftui-app/lessons/13)

---

<div class="post-metadata">

**Author:** ![awaitz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/awaitz/32/81494_2.png) [@awaitz](https://forums.kodeco.com/u/awaitz)\
**Post date:** [October 9, 2019, 4:51pm UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/2 "2019-10-09T16:51:45Z")

</div>

The final example code download for the “Observable Object” lesson seems to have a bug (at least on my machine, I am running Xcode 11.1). When you press the + button and add a new task, when the modal is dismissed the + button becomes unresponsive and you can’t display the new task modal dialog again. I noticed in the next lesson “Deleting From List” final download code, it has the same problem BUT if you try to delete a task (just initiate the delete but don’t commit it), the + button becomes functional again. I wonder if this is a bug in Xcode 11.1.

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [October 9, 2019, 7:42pm UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/3 "2019-10-09T19:42:35Z")

</div>

Thanks for reporting this!

This bug is new to me; I didn’t see it in the Xcode 11.0-11.1 beta lifecycle. But SwiftUI has been notorious for introducing new bugs with every new Xcode release. 😿 And it’s still occurring in the Xcode 11.2 / iOS 13.2 beta. I’ll report it to Apple and update the materials for these episodes if necessary.

---

<div class="post-metadata">

**Author:** ![monica](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monica/32/83328_2.png) [@monica](https://forums.kodeco.com/u/monica)\
**Post date:** [November 20, 2019, 10:12pm UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/4 "2019-11-20T22:12:39Z")

</div>

Thank you. I just wanted to report the bug is still happening as of November 20th in XCode Version 11.2.1 (11B500)/iOS 13.2. I hope it gets fixed soon!

---

<div class="post-metadata">

**Author:** ![jsiles42](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jsiles42/32/86717_2.png) [@jsiles42](https://forums.kodeco.com/u/jsiles42)\
**Post date:** [November 25, 2019, 1:06am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/5 "2019-11-25T01:06:19Z")

</div>

Thought I’d add that I was able to make the + button work by dragging in the window so that the Tasks heading disappeared off the top of the screen. When I let go the list slid back into place and the button was clickable again.

---

<div class="post-metadata">

**Author:** ![lywang](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lywang/32/81494_2.png) [@lywang](https://forums.kodeco.com/u/lywang)\
**Post date:** [November 25, 2019, 3:30am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/6 "2019-11-25T03:30:23Z")

</div>

I actually found a workaround which avoid using enviroment by setting the `modalIsPresented` binded with `NewTaskView`

Edit the original code as following:

Step 1: Make sure modalIspresented is a state variable.

Step 2: Pass this state variable to `NewTaskView`

```auto
NewTaskView(taskStore: self.taskStore, isPrensented: self.$modalIsPresented)

```

Step 3: Add property of this binding in `NewTaskView`

```auto
@Binding var isPrensented:Bool

```

Step 4: Replace the action of “Add” button to the following:

```auto
{
   self.taskStore.tasks.append(Task(name: self.text))
   self.isPrensented = false
}

```

Step 5: Delete unnecessary parts related with @Enviroment

Step 6: Preview may get crazy and can be fixed with

```auto
struct NewTaskView_Previews: PreviewProvider {
    static var previews: some View {
        NewTaskView(taskStore: TaskStore(), isPrensented: .constant(true))
    }
}

```

**Result:**

This workaround on my iphone 8 ios13.2 is bugless and works fine.

However, in simulator, this version and the originial version both suffer from having to drag the screen down a little bit to click the + button again.

The original version which I downloaded failed on my iphone 8 too.

I guess something is wrong with the simulator and the @Enviroment.

PS: I later learned the original dissmiss method is originally a simplification such that we do not need to pass the binding by ourselves. I hope it get fixed quickly.

---

<div class="post-metadata">

**Author:** ![huskar95](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/huskar95/32/81494_2.png) [@huskar95](https://forums.kodeco.com/u/huskar95)\
**Post date:** [November 27, 2019, 3:11am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/7 "2019-11-27T03:11:07Z")

</div>

Yep, that worked for me. Thanks. So weird though.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 11, 2020, 9:43pm UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/8 "2020-01-11T21:43:00Z")

</div>

@lywang Thank you for sharing your solution - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![alexz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/alexz/32/81494_2.png) [@alexz](https://forums.kodeco.com/u/alexz)\
**Post date:** [March 25, 2020, 10:52am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/9 "2020-03-25T10:52:47Z")

</div>

The error mentioned is still working. You are able to add a task. When you try to add the second one you get this error message:

**Can’t end BackgroundTask: no background task exists with identifier 2 (0x2), or it may have already been ended. Break in**.

Please fix asap.

---

<div class="post-metadata">

**Author:** ![johelzarco](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/johelzarco/32/81494_2.png) [@johelzarco](https://forums.kodeco.com/u/johelzarco)\
**Post date:** [March 28, 2020, 1:40am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/10 "2020-03-28T01:40:51Z")

</div>

I had the same problem in latest iOS-13.4 and Xcode-11.4. I tried many many things but the bug still appearing. Finally I solved the bug by just putting both views in the same file (ContentView and TaskView).  
But I guess it breaks the mvc ☹  
Here is my code in case someone find it usefull.

```
struct CoolTaskView: View {
    var myTaskStore : TaskStore
    @Binding var isPrensented : Bool
    @State var text : String = ""
    
    var body: some View {
        Form {
            TextField("Task name", text: $text)
            Button("Add"){
                self.myTaskStore.tasks.append(Task(name: self.text))
                //self.presentationMode.wrappedValue.dismiss()
                print(self.$text)
                self.isPrensented.toggle()
                print(self.myTaskStore.tasks)
            }
            .disabled(text.isEmpty)
        }
    }
}

struct ContentView: View {
    
    @ObservedObject var myTaskStore : TaskStore
    @State var modalIsPresented : Bool = false
    
    var body: some View {
        
        NavigationView {
            List{
                ForEach(myTaskStore.tasks) { task in
                Text(task.name)
                }
                .onMove{ sourceIndices, destinationIndex in
                    self.myTaskStore.tasks.move(fromOffsets: sourceIndices, toOffset: destinationIndex)
                }
                .onDelete{ indexSet in
                    self.myTaskStore.tasks.remove(atOffsets : indexSet)
                }
            }//list closure
                .navigationBarTitle("Tasks", displayMode: .inline)
                .navigationBarItems(
                    leading : EditButton(),
                    trailing:
                    Button(action: {
                        print("Add button has been pressed")
                        self.modalIsPresented.toggle()
                    }) {
                        Image(systemName: "plus")
                    }
            )
            .sheet(isPresented: $modalIsPresented) {
                CoolTaskView(myTaskStore: self.myTaskStore, isPrensented : self.$modalIsPresented)
            }
        }// NavView
    } // some View
}// ContentView : View

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 6, 2020, 6:20am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/11 "2020-04-06T06:20:32Z")

</div>

@alexz Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 6, 2020, 6:20am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/12 "2020-04-06T06:20:59Z")

</div>

@johelzarco Thank you for sharing your solution - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![kronos5807](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kronos5807/32/81494_2.png) [@kronos5807](https://forums.kodeco.com/u/kronos5807)\
**Post date:** [May 9, 2020, 8:54pm UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/13 "2020-05-09T20:54:35Z")

</div>

This behavior drove me crazy on this and other beginner SwiftUI projects. The behavior that requires us to swipe the list before we can use the add (+) is still present even using the latest available toolchains obtained today; the May 8th trunk of Swift 5.2 and the May 4th build of Swift 5.3 Development.

However, the other day I happened to come across a similar project where the “Add” button was placed outside the navigation bar; similar to Jessy’s “Add Task” button on the iPad demonstration. If we place it there, then everything appears to work for me; at least with the iPhone 11 Pro Max simulator, iPad Pro simulator, and my physical iPhone 11. Here is the modified NavigationView (placed inside of ContentView.swift). You may or may not like the changes to look and feel, and I don’t know what impact this will have later on in this course – but give it a try and see if it works for you too.

```auto
        NavigationView {
            VStack(alignment: .leading) {
                List(taskStore.tasks) { task in
                    Text(task.name)
                }
                Button(action: { self.modalIsPresented = true }) {
                    HStack {
                        Image(systemName: "plus.circle.fill")
                            .resizable()
                            .frame(width: 20, height: 20)
                        Text("New Task")
                    }
                }
                .padding()
                .accentColor(Color(UIColor.systemRed))
            } // VStack
            .sheet(isPresented: $modalIsPresented) {
                NewTaskView(taskStore: self.taskStore)
            }
            .navigationBarTitle("Tasks")
        } // NavView
[details="Summary"]
This text will be hidden
[/details]

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 3, 2020, 12:25am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/14 "2020-07-03T00:25:09Z")

</div>

@kronos5807 Thank you for sharing your solution - much appreciated!

---

<div class="post-metadata">

**Author:** ![shrutisharma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shrutisharma/32/81494_2.png) [@shrutisharma](https://forums.kodeco.com/u/shrutisharma)\
**Post date:** [July 9, 2020, 5:01am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/15 "2020-07-09T05:01:49Z")

</div>

The bug where ‘+’ button becomes unresponsive after adding a task on modal view both on simulator and iPhone as well is till present as of iOS 13.5.1 Xcode 11.5. Someone pointed out in the previous video the workaround/solution for this is to add the line below in the ContentView as well.

@Environment(.presentationMode) **var** presentation

This works in simulator as well as device.

Could the instructor please explain how does this make it work? Thanks.

---

<div class="post-metadata">

**Author:** ![j\_k\_nash](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/j_k_nash/32/106313_2.png) [@j\_k\_nash](https://forums.kodeco.com/u/j_k_nash)\
**Post date:** [August 17, 2020, 6:23pm UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/16 "2020-08-17T18:23:39Z")

</div>

I’m just going through the course now interestingly when I tried to add observable object in ContentView.Swift  
@ObservableObject var taskStore: TaskStore

I get unknown attribute ObservableObject

---

<div class="post-metadata">

**Author:** ![jcatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jcatterwaul/32/94210_2.png) [@jcatterwaul](https://forums.kodeco.com/u/jcatterwaul)\
**Post date:** [August 18, 2020, 9:23am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/17 "2020-08-18T09:23:57Z")

</div>

Did you maybe forget to `import Combine`?

---

<div class="post-metadata">

**Author:** ![j\_k\_nash](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/j_k_nash/32/106313_2.png) [@j\_k\_nash](https://forums.kodeco.com/u/j_k_nash)\
**Post date:** [August 18, 2020, 11:16am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/18 "2020-08-18T11:16:46Z")

</div>

Nope its clearly a bug because I copied the text from a completed project for just that line… and bingo it worked… Spelling and everything was the same!!! So very odd…

Thanks for the response… btw this part of the course scared the S@@T out of me… But I am persevering all the same 🙂

---

<div class="post-metadata">

**Author:** ![jorndorff](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jorndorff/32/92642_2.png) [@jorndorff](https://forums.kodeco.com/u/jorndorff)\
**Post date:** [October 4, 2020, 2:59am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/19 "2020-10-04T02:59:26Z")

</div>

I noticed that the app clears every time it is restarted. Should the code that we worked through in this program save the data locally each time it is closed and relaunched?

---

<div class="post-metadata">

**Author:** ![jcatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jcatterwaul/32/94210_2.png) [@jcatterwaul](https://forums.kodeco.com/u/jcatterwaul)\
**Post date:** [October 4, 2020, 4:08am UTC](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357/20 "2020-10-04T04:08:06Z")

</div>

This course does not feature any data persistence. Continue on to [Saving Data in iOS](https://www.raywenderlich.com/5429634-saving-data-in-ios), to add that feature!

The new version of _Your Second iOS and SwiftUI App_, which is in editing, does feature data persistence.

[Next page](https://forums.kodeco.com/t/your-second-ios-and-swiftui-app-observable-objects-raywenderlich-com/84357.md?page=2)
