Giter VIP home page Giter VIP logo

republished's Introduction

Republished

The @Republished proprty wrapper allows an ObservableObject nested within another ObservableObject to naturally notify SwiftUI of changes.

It was inspired by @mergesort's blog post introducing Boutique.

Problem

Nested ObservableObjects don't play well with SwiftUI.

An ObservableObject is a reference type, not a value type. This means a field on an outer ObservableObject containing an inner ObservableObject doesn't change when the inner object's one's changes. As such the outer object will not send the objectWillChange notification required for SwiftUI to know to rerender views that depend on its data.

Nested ObservableObjects are often a sign your data model needs some refactoring โ€” but it can also be a nice way to separate code concerns. Boutique provides a data store as a nestable ObservableObject. This repo's Example App uses nested ObservableObjects to separate its 'DomainModel' from its 'ViewModel', showcasing a version of the MVVM pattern that separates view-display logic from business logic (and is a bit closer to its original form).

Usage

The outer ObservableObject should hold the inner one in a var annotated with the @Republished property wrapper.

@Republished private var inner: InnerObservableObject

The inner ObservableObject's objectWillChange notifications will be re-emitted by the outer ObservableObject, allowing it to provide SwiftUI compatible accessors derived from the inner.

var infoFromInner: String { "\(inner.info)" }

Note: The outer ObservableObject will only republish notifications from inner ObservableObjects that it actually accesses.

SPM

You can use this library via Swift Package Manger by adding a dependency in your Package.swift.

.package(url: "https://github.com/adam-zethraeus/Republished", from: "1.0.1")

Example App

The RepublishedTestApp contains a simple example of an inner ObservableObject, used by an outer ObservableObject view model, to provide data for a regular SwiftUI View.

republished's People

Contributors

adam-zethraeus 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.