AttributeGraph: cycle detected through attribute and app get crushed in SwiftUI

When the htmlString size is not so big then it’s working. If size increased then app get crushed. Here is the code snipped. Code actually formate the hlml string and detected arabic text. Applied different font for arabic and other text. I am using “SwiftSoup” library for detect html tags.

struct PrayerLearningTopicByCategoryDetailsCard: View {
    @StateObject var prayerTopicByCategoryVM: PrayerLearningTopicByCategoryVM = PrayerLearningTopicByCategoryVM(prayerLearningAPIService: PrayerLearningAPIService())
    @EnvironmentObject var nav: NavigationStateManager
    @State var categoryName: String
    @State var categoryId: Int
    @State var isLoading: Bool = false
    var body: some View {
        VStack {
            if isLoading {
                LoaderView(isLoading: $isLoading)
            } else {
                ScrollView {
                    if let prayerTopicByCategoryData = prayerTopicByCategoryVM.prayerTopicByCategoryData {
                        VStack {
                            ForEach(prayerTopicByCategoryData) { data in
                                if let itemDetails = data.details, let title = data.title, itemDetails.count > 0 {
                                    PrayerTopicByCategoryDetailsView(title: title, details: itemDetails) 
                                }
                            }
                        }
                        .clipShape(RoundedRectangle(cornerRadius: 16))
                        .padding(.horizontal, 16)
                        .shadow(radius: 0.8)
                    }
                }
                .scrollIndicators(.hidden)
            }
        }
        .padding(.top, 12)
        .onAppear(perform: {
            isLoading = true
            Task {
                await prayerTopicByCategory(categoryId: categoryId)
                isLoading = false
            }
        })
    }
    
    func prayerTopicByCategory(categoryId: Int) async {
        await prayerTopicByCategoryVM.getPrayerTopicbyCategory(payload: PrayerLearningByCategoryPayload(language: AppData.shared.apiLanguage, category: categoryId))
    }
}

struct PrayerTopicByCategoryDetailsView: View {
    let title: String
    let details: [PrayerTopicByCategoryDataDetail]
    var body: some View {
        VStack {
            ForEach(details) { item in
                if let textInArabic = item.text {
                    DemoHtmlTextView(htmlString: textInArabic)
                }
            }
        }
    }
}

struct DemoHtmlTextView: View {
    @State var htmlString: String
    @State var attributedText: Text = Text("")
    @State var isFirstListItem: Bool = true
    var body: some View {
        VStack {
            attributedText
                .frame(maxWidth: .infinity, alignment: .topLeading)
        }
        .onAppear {
            DispatchQueue.main.async {
                self.extractAndClassifyText(from: htmlString)
            }
        }
    }
    
    func extractAndClassifyText(from html: String) {
        guard !html.isEmpty else {
            print("HTML string is empty")
            return
        }
        
        if !html.contains("<") || !html.contains(">") {
            DispatchQueue.main.async {
                attributedText = Text(html)
                    .foregroundColor(.deenBlack)
                    .font(.deenTextFontSemiboldBN)
            }
            return
        }

        do {
            let document = try SwiftSoup.parse(html)
            guard let body = try document.select("body").first() else {
                print("No body found in the HTML document")
                return
            }
            
            var composedText = Text("")

            let elements = body.children()
            
            for (index, element) in elements.enumerated() {
                let tagName = element.tagName()
                
                if tagName == "p" {
                    let paragraphText = try element.text().trimmingCharacters(in: .whitespacesAndNewlines)
                    composedText = addParagraph(paragraphText, isFirstElement: index == 0, composedText: composedText, element: element)
                }
                
                if tagName == "ul" {
                    let listItems = try element.select("li")
                    for listItem in listItems {
                        let listItemText = try listItem.text().trimmingCharacters(in: .whitespacesAndNewlines)
                        composedText = addListItem(listItemText, composedText: composedText, element: element)
                    }
                }
            }
            
            DispatchQueue.main.async {
                attributedText = composedText
            }

        } catch {
            print("Error parsing HTML or extracting text: (error.localizedDescription)")
        }
    }
    
    func addParagraph(_ text: String, isFirstElement: Bool, composedText: Text, element: Element) -> Text {
        var updatedText = composedText
        if !isFirstElement {
            updatedText = updatedText + Text("n")
        }

        let words = text.split(separator: " ")
        for (wordIndex, word) in words.enumerated() {
            let isBold = (try? element.select("strong").contains(where: { try $0.text().contains(String(word)) })) ?? false
            let isArabic = isArabicText(String(word))
            let styledWord = Text(String(word))
                .foregroundColor(.deenBlack)
                .font(isArabic ? Font.custom("AlQuran IndoPak by QuranWBW", size: 22) : .deenTextFontSemiboldBN)
                .fontWeight(isBold ? .bold : .regular)

            updatedText = updatedText + styledWord

            if wordIndex < words.count - 1 {
                updatedText = updatedText + Text(" ")
            }
        }
        return updatedText
    }

    func addListItem(_ text: String, composedText: Text, element: Element) -> Text {
        var updatedText = composedText
//        if isFirstListItem {
//            updatedText = updatedText + Text("n")
//        }
        isFirstListItem = false
        updatedText = updatedText + Text("• ").font(.headline)
        let words = text.split(separator: " ")

        for (wordIndex, word) in words.enumerated() {
            let isBold = (try? element.select("strong").contains(where: { try $0.text().contains(String(word)) })) ?? false
            let isArabic = isArabicText(String(word))
            let styledWord = Text(String(word))
                .foregroundColor(.deenBlack)
                .font(isArabic ? Font.custom("AlQuran IndoPak by QuranWBW", size: 22) : .deenTextFontSemiboldBN)
                .fontWeight(isBold ? .bold : .regular)

            updatedText = updatedText + styledWord

            if wordIndex < words.count - 1 {
                updatedText = updatedText + Text(" ")
            }
        }
        updatedText = updatedText + Text("n")
        return updatedText
    }

    func isArabicText(_ text: String) -> Bool {
        let arabicRange = text.range(of: "[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]", options: .regularExpression)
        return arabicRange != nil
    }
}

In console getting “AttributeGraph: cycle detected through attribute” message and code get crushed. But when html string size is small then it’s working fine

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật