Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI and The compiler is unable to type-check this expression in reasonable time

I am trying to implement a list view using SwiftUI and core data but am running into 'the compiler is unable to type-check this expression in reasonable time' error. The list contains 'lessons' and each 'type' of lesson leads to a different type of game, however, the error arises when I put in the if, else if conditions which lead to the relevant game link. I understand from other posts that the code should be broken down into simpler parts but am wondering what would be the best way to do this, especially for the condition statements. One way I have tried is by using specific @FetchRequests to extract one specific lesson at a time and while this works to populate the list, it creates unexpected behaviour when the entity is updated from another View. See here for more details of that issue: SwiftUI List View not updating after Core Data entity updated in another View. I'd be grateful for any suggestions on how to simplify this code or how to avoid this error. By the way, this error only occurs when using core data; when I 'hard code' the objects directly by using classes in a swift file, this error does not come about.

import SwiftUI
import CoreData
import UIKit

struct LessonList: View {

@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Lesson.entity(), sortDescriptors: []) var lessons: FetchedResults<Lesson>

var body: some View {

    NavigationView {

        List {
            Section(header: Text("Test")) {
                ForEach(lessons) { lesson in
                    if (lesson.stage == 1) && (lesson.type == "phonicIntro") {
                        NavigationLink(destination: PhonicIntroGame(lesson: lesson)) {
                            LessonRow(lesson: lesson)
                        }
                    } else if (lesson.stage == 1) && (lesson.type == "phonicDrag") {
                        NavigationLink(destination: PhonicDragGame(lesson: lesson)) {
                            LessonRow(lesson: lesson)
                        }
                    }
                }
            }
        }
        .navigationBarTitle("Lessons")            
    }
}//end of body
like image 559
Zenman C Avatar asked Mar 19 '20 16:03

Zenman C


1 Answers

Such error is a good indicator to break your view to simpler parts, like (only idea, `cause I can't compile this)

ForEach(lessons) { lesson in
    self.lessonRow(for: lesson)
}

...

private func lessonRow(for lesson: Lesson) -> some View {
  Group {
    if (lesson.stage == 1) && (lesson.type == "phonicIntro") {
        NavigationLink(destination: PhonicIntroGame(lesson: lesson)) {
            LessonRow(lesson: lesson)
        }
    } else if (lesson.stage == 1) && (lesson.type == "phonicDrag") {
        NavigationLink(destination: PhonicDragGame(lesson: lesson)) {
            LessonRow(lesson: lesson)
        }
    }
  }
}
like image 168
Asperi Avatar answered Sep 28 '22 09:09

Asperi