UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Reordering a CoreData entity bound List

Forums > SwiftUI

Hi,

Has anyone had any success re-ordering a List? I have a CoreData entity and whilst I can get the data to reorder and save back to CoreData .. the UI itself immediately jumps back to the original order when I hit save. Only when I leave the view and re-navigate to it, will the new sequence be shown.

Any ideas? I'm using the below to facilitate the re-ordering.

@ObservedObject var parent: Parent
...
private func onMove(source: IndexSet, to destination: Int) {
        var revisedItems: [Child] = parent.childRelationshipArray.sorted(by: { $0.order < $1.order }).map{ $0 }

        CoreDataHelper.executeBlockAndCommit {
            revisedItems.move(fromOffsets: source, toOffset: destination )

            for reverseIndex in stride(from: revisedItems.count - 1, through: 0, by: -1)
            {
                revisedItems[reverseIndex].order = Int32(reverseIndex)
            }
        }
    }

.. the view is being kept in sequence via a simple sort:

List {
    ForEach(parent.childRelationshipArray.sorted(by: { $0.order < $1.order }), id: \.id) { childEntity in
       ...
    }
}

4      

Scrub that; I found that I had to add an: xxx.objectWillChange.send() for it to paint the UI correctly. It works perfectly now :D

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.