Post

Replies

Boosts

Views

Activity

Reply to How to form a search predicate using swift data with a one to many model
Thanks for all your help on this and filling the ticket as well. Clearly there are some issues with the basic predicate not being able to do it as we first tried and having to move it. Let's hope they address that issue (it's still early beta so hopeful). Yes, it's a pain to do two separate sorts. This is the difference between simple todo list examples vs more real world use cases. Why have a sorted section list with orphan sections and not be able to sort out the irrelevant items in each section in the predicate searches. I guess part of this is that Apple still views these as basically persistent arrays and are having to do gymnastics to interrelate them. Why not just bite the bullet and provide a sql like interface over it? In the end it's a big improvement over core data and makes it way easier for developers to do a lot of the backend data management for apps not just view based data entry. The .listRowSeparator placement was me just being tired and not attentive when I was putting in the secondary filter. Thanks for being kind on pointing that out. I have some cleanup to do but this all worked in the end and I'll look for it to morph with each beta release. Eventually it will get there, right? I'll force unwrap just in case and I have some other tidbits of cleanup to do.
Jun ’23
Reply to How to form a search predicate using swift data with a one to many model
Your suggestion of moving it outside and gave me the expected result. So now I get the net sections per the filter which is good. While that gets my sections narrowed my model still has all the articles under each section. In the past I did this with a filter in the list (see commented out if statement): ForEach(result.toArticles!) { article in // if article.search.contains(filter) { NavigationLink(destination: ArticleView(title: article.title, summary: article.summary, content: article.body)) { VStack(alignment: .leading) { Text(article.title) .font(.custom("Helvetica", size: 16)) if isExpanded { Text(article.summary) .font(.custom("Helvetica", size: 12)) .foregroundColor(Color(UIColor.systemGray)) } } } // } /// Hide the row separator .listRowSeparator(.hidden) } but it barfs on the NavigationLink line of code with: "No exact matches in reference to static method 'buildExpression'" So I'm guess it's just still too early a beta here given the problem you originally located with the predicate and we just encounter problems further downstream until this is fixed. Or, under this model I have to find a way in the predicate as the second stage filter down to just the articles that in the toArticles that match to avoid using my simple "if" statement. Not sure how to do that in a predicate or if it's even possible.
Jun ’23
Reply to How to form a search predicate using swift data with a one to many model
Thank you partially helpful in getting past the errors so syntax is right but when I enter my search string (when it's 3 or more characters) and it kicks in using the predicate the result is no matches even though I know for certain that the search fields are present. If I don't run the predicate I get my full list with search entries (see screen shot) but if I type in a known string that's in the search string so should return a true (and maybe it does) I get nothing in the resulting sectionsDB. I've included some code here to show how I enter the predicate and pass it. Must be something down stream in "FilteredList" I need to do? struct FilteredList: View { @EnvironmentObject var userSettings: UserSettings @Environment(\.modelContext) private var context @Query() var sectionsSD: [SectionsSD] private var isExpanded: Bool var body: some View { List(sectionsSD) { result in Section(result.section) { ForEach(result.toArticles!) { article in NavigationLink(destination: ArticleView(title: article.title, summary: article.summary, content: article.body)) { VStack(alignment: .leading) { Text(article.title) .font(.custom("Helvetica", size: 16)) if isExpanded { Text(article.summary) .font(.custom("Helvetica", size: 12)) .foregroundColor(Color(UIColor.systemGray)) Text(article.search) } } } /// Hide the row separator .listRowSeparator(.hidden) } } } } init(filter: String, isExpanded: Bool) { if filter.count >= 3 { let searchPredicate = #Predicate<SectionsSD> { $0.toArticles.flatMap { $0.contains { $0.search.contains(filter) } } == true } _sectionsSD = Query(filter: searchPredicate) } self.isExpanded = isExpanded } }
Jun ’23