This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Requiered to use as @StateObject in a View for proper ownership. | |
extension ViewModel: ObservableObject {} | |
@Observable | |
class ViewModel { | |
/// Details | |
} | |
struct ContentView { | |
/// @Observed object |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentWrapperView: View { | |
@State var redrawTrigger: Bool = false | |
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() | |
var body: some View { | |
VStack { | |
Text("Redraw triggered with bool: \(redrawTrigger)") | |
ContentView() /// On every redraw, new viewModel instance is allocated in ContentView | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Observable | |
class ViewModel { | |
public var title: String | |
deinit { | |
print("deinit ViewModel") | |
} | |
init(title: String) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewModel: ObservableObject { | |
@Published public var title: String | |
deinit { | |
print("deinit ViewModel") | |
} | |
init(title: String) { | |
self.title = title |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AFTER | |
@main | |
struct BookReaderApp: App { | |
/// Object is allocated in a global context. | |
@State private var library = Library() | |
var body: some Scene { | |
WindowGroup { | |
LibraryView() | |
.environment(library) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import Foundation | |
import Observation | |
// A: Using specific class | |
/// Class that wraps withObservationTracking function into a Combine compatible continuos stream publisher | |
public class ObservationTracker<O: Observable & AnyObject, T> { | |
/// Subscribe to a publisher. | |
public var valuePublisher: AnyPublisher<T, Never> { |