Git Product home page Git Product logo

swiftui-shared-object's Introduction

SwiftUI SharedObject 🍱

@SharedObject is an alternative to @StateObject, @ObservedObject, @EnvironmentObject to handle ObservableObject. If you need to have multiple objects of the same class persisted among multiple view instances, it's difficult to handle the situation with other wrappers: with @StateObject the object will deinit with the view and be generated only for the specific view instance, with @EnvironmentObject you can bind only one instance of the same class for each Environment and with @ObservedObject is difficult to propagate object in nested views.

@SharedObject simply stores the objects using an identifier, so you can retrieve it each time you'll need it.

Usage

Retrieve the shared object with the given id or, if not present, create a shared object with an initial value:

@SharedObject("A") var letterA = Letter()

If you are sure that the object is already been created you can just retrieve the shared object:

@SharedObject("A") var letterA: Letter

You can give a default initial value to the class, so you don't need to specify in each view you think the object will be created:

final class Letter: SharableObject {

  var value: String
		
  init(_ value: String) {
	self.value = value
  }
		
  static var initialValue: Self {
	.init("A")
  }
}

Installation

  1. In Xcode, open your project and navigate to File β†’ Swift Packages β†’ Add Package Dependency...
  2. Paste the repository URL (https://github.com/lorenzofiamingo/swiftui-shared-object) and click Next.
  3. Click Finish.

Other projects

SwiftUI AsyncButton πŸ–²οΈ

SwiftUI MapItemPicker πŸ—ΊοΈ

SwiftUI PhotosPicker πŸŒ‡

SwiftUI CachedAsyncImage πŸ—ƒοΈ

SwiftUI VerticalTabView πŸ”

swiftui-shared-object's People

Contributors

lorenzofiamingo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

utsav475908

swiftui-shared-object's Issues

Odd thing in readme

"The problem is that with @StateObject the object will deinit with the view"

That is its main advantage so I'm intrigued to know why it's causing a problem for you.

Thanks for this wonderful sharedObject

As I mentioned on title, thank you for sharing this wonderful repo, and I'm currently using it in my code very happly.
However I found that there's a part that printing the typeof ObjectType everytime I interact with it. Could you please remove the 15th line on SharedRepository.swift

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.