Post

Replies

Boosts

Views

Activity

How to GROUP and SORT fetched core data, and create a List in order?
I have a core data:entity: Songsattributes: singer, songIf I want to GROUP & SORT the fetched core data, and make a List---(Section: Singer), (Item: Song), how can I do it?import SwiftUIimport CoreDatastruct SongList: View { @FetchRequest(entity: Songs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Songs.song, ascending: true)]) var tutorial: FetchedResults<Tutorial>func group(_ result : FetchedResults<Songs>)-> [[Songs]] { return Dictionary(grouping: result) { (element : Songs) in element.singer!.sorted() } .values.map{$0} }Using the function, the fetched core data ("singer"), can be grouped and use in the Section of my List. However, the main probem is the order is arbitrary every time, as Dictionary is "unordered". How can I make it in order, if keep using Dictionary? Or, is there any other solution?The fetched core data ("song"), is in order, in the Item of my List.
9
0
4.6k
Feb ’20
Update SwiftUI View after updating core data value with toggle button
In SwiftUI, I have a simple core data entity, and one of the attributes called "finished" (Bool).I fetch the core data to create a List in ListView. When I click an item of the List, it goes to the DetailView, which includes the record "finished".In the DetailView, I added a toggle button to SHOW the value of "finished", and meanwhile I use this toggle button to UPDATE the record of "finished" in core data.The curent problem is I can use this toggle button to CHANGE the record in core data; however, the result CANNOT be updated immediately in DeailView. (That means, when I go back to ListView, I see it's updated. And when I click the item and go to DetailView, the record's also properly shown.)1) How to fix this issue? What's the best way to do it?2) What's the proper way to assign the core data value of "finished" to the toggle button, when DetailView's launched? I'm new to Swift, and very grateful for anybody's help.
3
0
6.3k
Mar ’20