All Projects → cookednick → Observable

cookednick / Observable

Licence: MIT license
A generic ObservableObject for every property!

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Observable

E-Rezept-App-iOS
https://gematik.github.io/E-Rezept-App-iOS/
Stars: ✭ 76 (+85.37%)
Mutual labels:  combine, swiftui
SwiftUIViewRecorder
Efficiently record any SwiftUI View as image or video
Stars: ✭ 20 (-51.22%)
Mutual labels:  view, swiftui
Weather
A simple SwiftUI weather app using MVVM.
Stars: ✭ 23 (-43.9%)
Mutual labels:  combine, swiftui
Easify-iOS
An iOS application to test out Spotify API. It uses SwiftUI and Combine.
Stars: ✭ 15 (-63.41%)
Mutual labels:  combine, swiftui
99StocksSwiftUI
SwiftUI app that fetches a list of companies, sort them by their share price and can show its details on a separate view
Stars: ✭ 34 (-17.07%)
Mutual labels:  combine, swiftui
Notflix
📱Netflix like application using SwiftUI and Combine
Stars: ✭ 76 (+85.37%)
Mutual labels:  combine, swiftui
TabBar
📱 TabBar – highly customizable tab bar for your SwiftUI application.
Stars: ✭ 105 (+156.1%)
Mutual labels:  view, swiftui
NYTimes-iOS
🗽 NY Times is an Minimal News 🗞 iOS app 📱 built to describe the use of SwiftSoup and CoreData with SwiftUI🔥
Stars: ✭ 152 (+270.73%)
Mutual labels:  combine, swiftui
MultiModal
Use multiple .sheet, .alert, etc. modifiers in the same SwiftUI View
Stars: ✭ 49 (+19.51%)
Mutual labels:  view, swiftui
SwiftReactor
A protocol which should help structure your data flow in SwiftUI (and UIKit).
Stars: ✭ 57 (+39.02%)
Mutual labels:  combine, swiftui
GITGET
GitHub의 Contributions를 iOS의 Widget으로 보여주는 App
Stars: ✭ 101 (+146.34%)
Mutual labels:  combine, swiftui
JewelCase
This is the source code for JewelCase, a sample app demonstrating how to use SwiftUI and Firebase together. This slide deck discusses the architecture of the app: https://www.slideshare.net/peterfriese/building-swiftui-apps-with-firebase
Stars: ✭ 42 (+2.44%)
Mutual labels:  combine, swiftui
mocka
Mocka — A Mock Server Made for Developers by Developers, made in Swift ❤️
Stars: ✭ 56 (+36.59%)
Mutual labels:  combine, swiftui
NetworkAgent
This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of dependencies and works with Combine api + Codable
Stars: ✭ 16 (-60.98%)
Mutual labels:  combine, swiftui
Luna
Tracking the moon phase using SwiftUI and Combine
Stars: ✭ 19 (-53.66%)
Mutual labels:  combine, swiftui
Swiftui Tutorials
A code example and translation project of SwiftUI. / 一个 SwiftUI 的示例、翻译的教程项目。
Stars: ✭ 1,992 (+4758.54%)
Mutual labels:  combine, swiftui
SwiftUI-MVVM-C
An iOS template project using SwiftUI, Combine and MVVM-C software architecture
Stars: ✭ 85 (+107.32%)
Mutual labels:  combine, swiftui
Francis
Bonjour browser for macOS and iOS
Stars: ✭ 25 (-39.02%)
Mutual labels:  combine, swiftui
clouds
🌦 A weather app for iOS, written in SwiftUI.
Stars: ✭ 26 (-36.59%)
Mutual labels:  combine, swiftui
iOS-App
🕹️ iOS application of HardcoreTap game
Stars: ✭ 17 (-58.54%)
Mutual labels:  combine, swiftui

Observable

A property wrapper type that creates an ObservableObject for just a single property.

This can have enormous performance benefits due to how SwiftUI recomputes views.

SwiftUI + Performance

Views get called to update each time any of an object's properties change. Even if it's irrelevant to a given view.

Consider the following common pattern of a typical custom view with one @State property.

struct BedroomLabel: View {
	@State var areTheLightsOn = false
	var numberOfPeople = 0
	
	var body: some View {
		VStack {
			Text("This bedroom has \(numberOfPeople) people in it.")
			
			Toggle("Lights On/Off", isOn: $areTheLightsOn)
		}
	}
}

This is a very common pattern in SwiftUI, and it works alright, but the BedroomLabel will update in its entirety whenever the lights get toggled. This means the CPU spends unnecessary time checking the entire body whenever this happens.

How can we do better?

One ObservableObject per Property

Here is that same example written using Observable.

struct BedroomLabel: View {
	@Observable var areTheLightsOn = false
	var numberOfPeople = 0
	
	var body: some View {
		VStack {
			Text("This bedroom has \(numberOfPeople) people in it.")
			
			Observing(bindingOf: $areTheLightsOn) { isOn in
				Toggle("Lights On/Off", isOn: isOn)
			}
		}
	}
}

@Observable writes just like @State, except it stores the value in an implicit generic ObservableObject type. Observing takes care of unwrapping that and recomputing a view closure whenever its changed.

This means BedroomLabel won't be computed on changes. Only the Toggle will be.

ObservableSample is a sample app that demonstrates, side-by-side, the performance gains of using @Observable instead of @State.

Installation

Use Swift Package Manager with this GitHub URL.

License

MIT License

Copyright © 2021-2022 Nicolas Cook Leon

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].