Group Group Group Group Group Group Group Group Group

Your Second iOS and SwiftUI App · Binding | raywenderlich.com

@jessycatterwaul Thanks for your prompt response! You clarified a lot for me but I am still confused on my experiment to pass down a “task class” and edit it. When I do this in the “RowView.swift file” like so:

	struct RowView: View {

	var task: Task

	var body: some View {
		Button(action: { self.task.name = "This new name should be shown but is not!" }) {
			Text(task.name).strikethrough(task.completed)
		}


	}
}

The new name is not updated in the list. It will be however, if I open the the NewTaskView, which forces a re-render.

I don’t understand how ObservedObject can listen to the appending of the array (without a binding), but cannot listen to the change of a reference type’s property in that array.

Are you using the 11.4 beta? That’s not the behavior I get. Only the new rows re-render.

In order to see what you’re seeing, I need to change the Identifiable behavior of Task. Again, this is all subject to change. Don’t put any faith in SwiftUI magically working, when there are ways to force it to work.

Arrays are value types. @Published is just a shortcut to calling objectWillChange in the array’s willSet observer (with some async magic to make it more like didSet).

@jessycatterwaul Maybe this is because I’m displaying RowViews like this?

ForEach(
  Array( taskStore.tasks.enumerated() ),
  id: \.element.id
) { index, _ in
  RowView(task: self.taskStore.tasks[index])
}

I wouldn’t think that would result in anything different. You’re still using the same id. I don’t know what the rest of your project looks exactly like, though.

@jessycatterwaul Maybe my Task.swift is the problem?


import Foundation

class Task: Identifiable {
	let id = UUID()

	var name: String
	var completed: Bool = false

	init(name: String) {
		self.name = name
	}
}

I just want to understand why changing a Task class doesn’t cause the row to be updated, but appending to the task list does?

I think I may understand why it’s not updating when the class gets updated. As this StackOverflow post explains: https://stackoverflow.com/questions/57459727/why-an-observedobject-array-is-not-updated-in-my-swiftui-application.

“Since classes are references the array remains unchanged and so nothing is emitted by the subject.”

Does this sound correct to you?

Yes! :+1:

That’s why I mentioned above that Swift Arrays are value types. I didn’t realize that you didn’t know about what you found at that link, which is the converse of that. If you reassign a reference in an Array, that’s a mutation of the Array. But if you mutate any property of a reference in an Array, it’s not.

More on this here. Let me know if you think there’s anything missing that we don’t cover, which would have helped you. (I realize the third link is after this course, in the learning path.)

1 Like

I have a question to a bug I found.

When I add a Task and after adding immediately I try to use the Edit Button, the Rows don’t change to the edit mode. A weird fading happens.

What could be the reason? It happens also with the code for this course if I download it.

Thank you

Hi!

I’d like to see a video of this “weird fading”. As you can see in the comments for most of the videos, all of us are having an issue after adding a task, but I haven’t heard about fading yet!

Hey Jessy,

thank you for the reply. Here is the video.

https://www.dropbox.com/sh/ieefynicdv2j9t5/AACV9P2-p41BWYrVvVzp3Fj2a?dl=0

I couldn’t upload it here because I am still new.

I had the behavior in the simulator in my own app when using exactly your codes of the tutorial.

And here some error messages… maybe they help?

2020-04-27 21:06:07.117449+0200 TaskList[1439:62793] -[_UIRemoteKeyboards proxy]_block_invoke Failed to access remote service: Error Domain=NSCocoaErrorDomain Code=4099 “The connection to service on pid 1336 named com.apple.UIKit.KeyboardManagement.hosted was invalidated from this process.” UserInfo={NSDebugDescription=The connection to service on pid 1336 named com.apple.UIKit.KeyboardManagement.hosted was invalidated from this process.}

2020-04-27 21:06:07.126861+0200 TaskList[1439:57227] [Snapshotting] Snapshotting a view (0x7f8b36082600, UIKeyboardImpl) that is not in a visible window requires afterScreenUpdates:YES.

Thank you a lot for looking into it.

Joern

1 Like

Ah! I never looked at it in the canvas preview. That doesn’t happen in the Simulator or on-device. But what does happen, as others have noticed, is probably worse.

Hey Jessy,

I also uploaded a video from the simulator. There is the same behavior as on the canvas.

https://www.dropbox.com/s/eew4tvpltet7b2v/IMG_0873.MOV?dl=0

Is this behavior connected to the “known” problem?

So no solution so far?

Thank you very much for taking the time.

Joern

Interesting!

The rest of us who have commented aren’t seeing that outside of the canvas. Instead, we’re just getting lack of responsiveness.

No solution yet. Here’s hoping it’s less than a month and a half away. :crossed_fingers:

I’m seeing the same crash:

Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444

I’m not using the extension. I’m using the simpler method recommended in the comments.

So when we want to make our own apps, do we have to have those kinds of extensions in our project? I didnt quite understand what purpose those extensions played. Why did the errors go away?

I’m running into this same issue. Not sure what I’m doing wrong. I even download the materials and upload the files from the tracklist and still get this error. Any idea what could be wrong or how I can fix it to move on with the rest of the course.

Hi , How did you solve this crash. Even my app is crashing because of same issue. Something related t o arrays.

@krishnabpm Do you still have issues with this?