Git Product home page Git Product logo

shufflingb / swiftui-foreach-drag-core-data Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 1.0 701 KB

iOS SwiftUI app that demonstrates the ForEach component being used with a Core Data Entity that has a Many-to-Many relationship with itself AND uses Xcode's data modeller Arrangement Ordered option to facilitate arbitrary ordering of those relationships.

License: MIT License

Swift 100.00%
swiftui-example coredata foreach draggable

swiftui-foreach-drag-core-data's Introduction

Intro

This is an iOS SwiftUI app that demonstrates the ForEach component being used with a Core Data Entity that has a Many-to-Many relationship with itself and uses Xcode's data modeller Arrangement Ordered option to facilitate arbitrary ordering of those relationships.

Why this rather than directly storing the order as a "priority" attribute on the entity? Storing as part of the relationship is more flexible; it allows ordering as part of many lists without requiring additional Entity "priority" attributes to be added for each list.

NB: Do not adopt this approach if intending to use CloudKit.

CloudKit does not support the use of OrderedSets, see here and here for more on that

Installation

Works for me (tm) with Xcode 11.6 and iOS 13.5

Running

Should run on any iOS 13.5+ simulator or device.

  • Tap "Add an item" to add an item.
  • Swipe left to delete an item.
  • Tap "Edit" to delete and rearrange items and "Done" when finished.
  • Tap "Save Changes?" to persist changes to Core Data backend.
  • Uninstall to remove app and its data.

Notes

Data Model

There's only one Core Data entity called an Item. This Entity has:

  • A title string
  • UUID attribute which it it constrained to be unique by.
  • A Root flag boolean to indicate if the Item is the Root item from which all others are its children.
  • childrenList and parentList relationships with itself that are the inverses of each other.
  • It's been configured so that Codegen is manual.

Item Core Data Entity

The Arrangement Ordered option has been ticked for the children and parent relationships. Which, according to official documentation does the following: "Arrangement: Select the Ordered checkbox to specify that the relationship has an inherent ordering, and to generate an ordered mutable set.".

Item Core Data Relationship

The Editor> Create NSManagedObjectSubclass Subclass... was then used to make the Core Data class boiler plate easier to inspect and play with (not that I did much of that). This is in Item+CoreDataClass.swift and Item_CoreDataProperties.swift

Bug?

I think either the official documentation might be wrong or the code created by Editor> Create NSManagedObjectSubclass Subclass... is a bit broken. As for both the parent and children lists, the generated code it produces in Item_CoreDataProperties.swift ends up using the immutable NSOrderedSet type (and not a mutable type such as NSMutableOrderedSetthat the documentation suggests it should be).

I tried working around this by casting to a mutable NSMutableOrderedSet, but the framework does not recognise the changes and silently drops them. There's a copy of this attempt commented out in the code. In the end what did work was to create a copy of the list to aNSMutableOrderedSet, reorder and then replacing.

If I can clarify further I will update here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.