Post

Replies

Boosts

Views

Activity

Reply to FetchedResults to Array (Error)
You can't use @FetchRequest in a function, but you absolutely can use a fetch request in a function. You just have to build an NSFetchRequest object, set its predicate and sort descriptors, then fetch it from a managed object context. Depending on exactly where that function is in your code, you may be able to access the managed object context without passing it explicitly into the function. It would go a bit like this: func searchResults(searchingFor: String) -> [Recipe] { let recipeFetch = Recipe.fetchRequest() recipeFetch.predicate = NSPredicate(format: "title CONTAINS[c] %@",searchingFor) recipeFetch.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)] let results = (try? self.managedObjectContext.fetch(recipeFetch) as [Recipe]) ?? [] return results } I haven't run this exact code, but it's very similar to code I'm using in several of my applications.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’22
Reply to creating a list from an array of structs
The problem is your view's newdrug value always has the same UUID. It gets allocated one time when the view is instantiated. You then add the item with that UUID to the list several times. In your add button, replace this: viewModel.favs.insert(newdrug, at: 0) with this: viewModel.favs.insert(Drug(name: newdrug.name, amount: newdrug.amount, bag: newdrug.bag), at: 0) That will cause a whole new Drug object to be created and inserted. It gets a new UUID when it's created, so it won't match the ID of any of the other objects in the array. SwiftUI will then be able to recognize it's a different object.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to Display a list of values in an NSTableView/NSOutlineView cell?
I've managed to improve performance somewhat. Modifications are in the most recent commits on the repo. Added a property to my Core Data objects which computes the height of that row from only the object count in the relevant fields Implemented NSOutlineViewDelegate.outlineView(_:heightOfRowByItem:) to get the height from the object Set up my outline view delegate to observe NSManagedObjectContext, get the index of every row the outline view is showing, and tell the outline view to recompute those rows' heights Disabled usesAutomaticRowHeights Before the modifications, I got 172ms mean hitch duration, 168ms median. Afterwards, I get 87ms mean 88ms median. Scrolling performance still isn't great, but that may be down to my development machine being on the older side.
Topic: UI Frameworks SubTopic: AppKit Tags:
Aug ’22