LiYanan2004 / MarkdownView

Rendering Markdown text natively in SwiftUI.
https://liyanan2004.github.io/MarkdownView/documentation/markdownview/
MIT License
195 stars 18 forks source link

MarkdownView sometimes crashes #8

Closed Strider-Alex closed 1 year ago

Strider-Alex commented 1 year ago

Xcode version: 14.3 Platform: iOS 16.3.1

Problem: MarkdownView sometimes crashes. Seems to be some timing issue. I need to open and close the view that has MarkdownView back and forth to reproduce the issue. Seems to occur more often you move the app from background to foreground.

When debugging on device, the code crashes here:

Screenshot 2023-05-31 at 6 31 41 PM

Debug console prints a lot of warnings: === AttributeGraph: cycle detected through attribute xxxxxx ===

LiYanan2004 commented 1 year ago

I think that's a bug of SwiftUI. It occurs randomly.

LiYanan2004 commented 1 year ago

I noticed this bug may be related to markdown Tables. Can you share your Table used in this Markdown?

Strider-Alex commented 1 year ago

@LiYanan2004 In my case there's no table. Only text, ordered list and url links.

LiYanan2004 commented 1 year ago

@Strider-Alex Can you provide a minimum, reproducible snippet of code?

Strider-Alex commented 1 year ago

Sure. After some experiment I'm able to reproduce in the following view:

struct TestView: View {
    @State var text = "1. [test](test.com)\n 1. [test](test.com)\n 1. [test](test.com)"
    var body: some View {
        ScrollView { // this scroll view seems to be the issue
            MarkdownView(text: $text)
                .textSelection(.enabled)
        }
    }
}
LiYanan2004 commented 1 year ago

@Strider-Alex One more question. Is this crash happens on simulators or physical devices, and have you tried on iOS 16.4? I have tried on my side with Xcode 14.3 and 16.4 simulator. Works fine (also tried open and close the app).

LiYanan2004 commented 1 year ago

@Strider-Alex Xcode 14.3.1 released. You can try to update your Xcode to see if the bug resolved.

Strider-Alex commented 1 year ago

The crashes happened on physical devices and occurred for multiple iOS versions. Can you wrap the TestView inside a NavigationLink and try to open & close it multiple times? That's how I reproduce the issue.

LiYanan2004 commented 1 year ago

Gotcha. Reproduced. I will investigate this bug.

LiYanan2004 commented 1 year ago

Hello @Strider-Alex

I'm happy to tell you I may have resolved your issue. I improve the implementation of ._task which can back-deployed to older OS. Seems like the === AttributeGraph: cycle detected through attribute xxxxxx === bug resolved and I have run an automation to open & close the navigation link for 5 minutes with no crashes and no AttributeGraph cycle warnings.

You can check it out at fix-graph-cycle branch.

If you are building an package, update your Package.swift with .package(url: "https://github.com/LiYanan2004/MarkdownView.git", .branch("fix-graph-cycle")) to check it out.

Don't forget to reply me whether the bug is fixed or not. Thanks for this great issue.

Strider-Alex commented 1 year ago

Hey @LiYanan2004, this is awesome. I don't have chance to test for myself recently, but I'm total okay if the issue is fixed in your automation.

LiYanan2004 commented 1 year ago

OK I'll merge the fix and if the bug still happens on your side after updating to the latest version, you can reopen the issue.