I'm working to implement an infinite scrolling view using @SectionedFetchRequest. I'm able to update Core Data to reflect the new sections and order-within-the-section. @SectionedFetchRequest is correctly fetching and organizing everything on launch, but it's not updating as I update Core Data. I am using a SQLite app to simulate the Core Data query, and I can see @SectionedFetchRequest isn't picking up on everything that's there. Any thoughts as to what might be going on?
extension CDArticle {
static func displayFetchRequest() -> NSFetchRequest<CDArticle> {
let request: NSFetchRequest<CDArticle> = fetchRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \CDArticle.sectionFolder, ascending: true),
NSSortDescriptor(keyPath: \CDArticle.sectionOrder, ascending: true)]
request.predicate = [ICPredicate.fetched].andPredicate()
return request
}
}
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@ObservedObject var controller = ArticleController.shared
@SectionedFetchRequest(fetchRequest: CDArticle.displayFetchRequest(),
sectionIdentifier: \CDArticle.sectionFolder,
transaction: Transaction(animation: .spring()))
private var fetchedSections
@State var queue = DispatchQueue.global(qos: .userInitiated)
init() {
detach {
await FolderFetcher.shared.fectchNewSections()
}
}
var body: some View {
ScrollViewReader { scrollProxy in
List {
ForEach(fetchedSections) { section in
Section(header: Text(FolderFetcher.getSectionName(section.id ?? "Nil") ?? "Nil")) {
ForEach(section) { article in
ArticleView(article: article)
.setLocationReaderKey(id: article.googleID!)
.id(article.googleID)
.onAppear {
if let topArticleID = topMostArticle {
scrollProxy.scrollTo(topArticleID, anchor: .top)
}
}
}
}
}
}
}
}
}