Git Product home page Git Product logo

berkus / swiftui-core-data-with-model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shufflingb/swiftui-core-data-with-model

0.0 1.0 0.0 166 KB

Demo/test of easier to maintain Core Data processing in SwiftUI applications by abstracting Core Data processing away from the UI and into its own App Model while maintaining an ease of similar to the built in @FetchRequest

License: MIT License

Swift 100.00%

swiftui-core-data-with-model's Introduction

Intro

TL;DR; Demo/test project showcasing easier to maintain Core Data processing in SwiftUI applications by abstracting Core Data processing away from the UI and into its own App Model.

Why

Good SW engineering practice seeks to achieve a Separation of Concerns. As of 10th of August 2020, the 'normal' SwiftUI approach to integration of Core Data involves data operations (using @FetchRequest and Core Data's Managed Object Context) within the immediate hierarchy of the View components that consume and create that same data.

As convenient as it is to work with initially (particularly @FetchRequest) - splattering Core Data and associated processing throughout an application's UI components disobeys the Separation of Concerns and is liable to result in apps that are difficult maintain.

What's demonstrated

Alongside the conventional @FetchRequest/MOC approach for comparison, this project demonstrates how Core Data can be handled within an app's model, thereby separating data and UI concerns, while still maintaining ease of use.

  • Alternative model based Core Data integration that:
    1. Keeps data synchronised across the views using a Combinepublisher for the Core Data results.
    2. Is almost as convenient to work with as @FetchRequest
  • A conventional @FetchRequest Core Data integration for comparison.
  • How to setup Core Data using Xcode 12 and its SwiftUI App Life Cycle template I.e. without an AppDelegate.swift and SceneDelegate.swift to setup the Core Data persistentContainer, but with a model and the the usage of @StateObject to do the same.
  • Use of the iOS14/SwiftUI's @StateObject, @Environment, @EnvironmentObject, @ObservedObjectand@FetchRequest` property wrappers.

Installation

This project requires at least iOS14 and Xcode 12 (I used Xcode 12.0 beta 4)

Running/testing

Should run on any iOS 14+ simulator or device.

When it runs, the app will display summary data about the number of Item entities it has in the system in various View components.

App running

Components are named after how they get their data, I.e. either from:

  1. The Model
  2. Or by using the 'normal' @FetchRequest and MOC approach

When normal and flagged Items are Added, the data across all of the View components should remain the same.

Notes

  1. The code contains copious comments and should be pretty self explanatory.
  2. Kudos and thanks to Apostolos Giokas for his excellent article on Medium that contained the Core Data Combine publisher class implementation.

swiftui-core-data-with-model's People

Contributors

shufflingb avatar

Watchers

 avatar

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.