The weirdest bug/crash in widgets?
Mysterious crash and ChatGPT to the rescue.
Published: Feb. 14, 2023 Sponsored App StoreSo today, my day got quite derailed when I noticed an issue with one of SwitchBuddy countdown widgets. The game came out today, but the widget was showing “Tomorrow”. At first, I thought that my countdown logic is just slightly off in this case, that I previously did not notice.
Then what started happening is that when I removed the widget and added it again, it wouldn’t load the data for the game that was coming out at all. It was showing placeholder content.
I thought the problem is in my code that perhaps filters out already released games since these don’t make much sense in the countdown widgets. I tweaked some things around but no luck. Further testing showed that games that were already released also worked fine. Every game worked fine except those that were just released today.
I had no idea and decided to try debugging with the Console.app. And after some digging, I finally noticed that the widget was crashing. Perhaps this is a behavior that other devs know - when the widget crashes it displays the placeholder content. But I thought the problem is in my entries and the underlying data.
At least with the Console.app I finally had an error message. Super weird, but error message nonetheless.
Request ended for countdown:systemMedium - error: SwiftUI.(unknown context at $1d9616ca0).CodableTextAttachment.Error.invalidAttachment - The operation couldn’t be completed. (SwiftUI.(unknown context at $1d9616ca0).CodableTextAttachment.Error error 0.)
I wasn’t able to google anything about this issue. What the hell is CodableTextAttachment
anyway?
Since the widget worked fine with other games, it was puzzling why there should be a SwiftUI issue.
I decided to consult ChatGPT. I did not expect any help but I was curious. The first reply was generic, but the second pointed me toward text views.
CodableTextAttachment
is a type defined in the SwiftUI framework that represents an attachment to a text view. It conforms to the Codable protocol, which means that it can be encoded and decoded using the Encoder and Decoder protocols. This allows it to be used in SwiftUI views that require data to be encoded and decoded, such as widgets. CodableTextAttachment is used to display images, videos, and other types of media within a text view in a SwiftUI app.
And sure enough, when I checked the line responsible for showing the text info, I saw:
Text(Image(systemName: "sparkes")) + Text("Today!")
For some reason, seconds after seeing this line, I was sure this was the issue that had been crashing my widget. When the release date is not today, the widget displays just single Text
with the appropriate info.
And sure enough, I removed the Image
part, and widgets started working flawlessly for just-released games. 😵💫
I wrote this post mainly that if someone runs into the error above, my blog post should be reachable via search. And also to remind me that check those places in your code that “totally work and don’t need checking”.