@@ -207,7 +207,7 @@ private final class ObservableStoragePublisher<ObjectType>: Publisher where Obje
207207 if value. realm != nil && !value. isInvalidated, let value = value. thaw ( ) {
208208 // This path is for cases where the object is already managed. If an
209209 // unmanaged object becomes managed it will continue to use KVO.
210- let token = value. _observe ( keyPaths, subscriber)
210+ let token = value. _observe ( keyPaths, subscriber)
211211 subscriber. receive ( subscription: ObservationSubscription ( token: token) )
212212 } else if let value = unwrappedValue, !value. isInvalidated {
213213 // else if the value is unmanaged
@@ -222,6 +222,9 @@ private final class ObservableStoragePublisher<ObjectType>: Publisher where Obje
222222 let subscription = SwiftUIKVO . Subscription ( observer: kvo, value: value, keyPaths: keyPaths)
223223 subscriber. receive ( subscription: subscription)
224224 SwiftUIKVO . observedObjects [ value] = subscription
225+ } else {
226+ // As SwiftUI calls this method before we setup the value, we create an empty subscription which will trigger an UI update when `send` gets called, which will call call again this method and allow us to observe the updated value.
227+ subscriber. receive ( subscription: ObservationSubscription ( ) )
225228 }
226229 }
227230}
@@ -231,10 +234,8 @@ private class ObservableStorage<ObservedType>: ObservableObject where ObservedTy
231234 @Published var value : ObservedType {
232235 willSet {
233236 if newValue != value {
234- objectWillChange. subscribers. forEach {
235- $0. receive ( subscription: ObservationSubscription ( token: newValue. _observe ( keyPaths, $0) ) )
236- }
237237 objectWillChange. send ( )
238+ self . objectWillChange = ObservableStoragePublisher ( newValue, keyPaths)
238239 }
239240 }
240241 }
@@ -405,14 +406,12 @@ extension Projection: _ObservedResultsValue { }
405406///
406407/// Given `@ObservedResults var v` in SwiftUI, `$v` refers to a `BoundCollection`.
407408///
408- @available ( iOS 13 . 0 , macOS 10 . 15 , tvOS 13 . 0 , watchOS 6 . 0 , * )
409+ @available ( iOS 13 . 0 , macOS 11 . 0 , tvOS 13 . 0 , watchOS 6 . 0 , * )
409410@propertyWrapper public struct ObservedResults < ResultType> : DynamicProperty , BoundCollection where ResultType: _ObservedResultsValue & RealmFetchable & KeypathSortable & Identifiable {
410411 private class Storage : ObservableStorage < Results < ResultType > > {
411412 var setupHasRun = false
412413 private func didSet( ) {
413- if setupHasRun {
414- setupValue ( )
415- }
414+ setupValue ( )
416415 }
417416
418417 func setupValue( ) {
@@ -453,6 +452,30 @@ extension Projection: _ObservedResultsValue { }
453452 }
454453
455454 var searchString : String = " "
455+
456+ init ( _ results: Results < ResultType > ,
457+ configuration: Realm . Configuration ? = nil ,
458+ filter: NSPredicate ? = nil ,
459+ where: ( ( Query < ResultType > ) -> Query < Bool > ) ? = nil ,
460+ keyPaths: [ String ] ? = nil ,
461+ sortDescriptor: SortDescriptor ? = nil ) where ResultType: Object {
462+ super. init ( results, keyPaths)
463+ self . configuration = configuration
464+ self . filter = filter
465+ self . where = `where`
466+ self . sortDescriptor = sortDescriptor
467+ }
468+
469+ init < ObjectType: ObjectBase > ( _ results: Results < ResultType > ,
470+ configuration: Realm . Configuration ? = nil ,
471+ filter: NSPredicate ? = nil ,
472+ keyPaths: [ String ] ? = nil ,
473+ sortDescriptor: SortDescriptor ? = nil ) where ResultType: Projection < ObjectType > , ObjectType: ThreadConfined {
474+ super. init ( results, keyPaths)
475+ self . configuration = configuration
476+ self . filter = filter
477+ self . sortDescriptor = sortDescriptor
478+ }
456479 }
457480
458481 @Environment ( \. realmConfiguration) var configuration
@@ -524,10 +547,10 @@ extension Projection: _ObservedResultsValue { }
524547 keyPaths: [ String ] ? = nil ,
525548 sortDescriptor: SortDescriptor ? = nil ) where ResultType: Projection < ObjectType > , ObjectType: ThreadConfined {
526549 let results = Results< ResultType> ( RLMResults< ResultType> . emptyDetached( ) )
527- self . storage = Storage ( results, keyPaths )
528- self . storage . configuration = configuration
529- self . filter = filter
530- self . sortDescriptor = sortDescriptor
550+ self . storage = Storage ( results,
551+ configuration: configuration ,
552+ filter: filter ,
553+ sortDescriptor: sortDescriptor )
531554 }
532555 /**
533556 Initialize a `ObservedResults` struct for a given `Object` or `EmbeddedObject` type.
@@ -547,10 +570,11 @@ extension Projection: _ObservedResultsValue { }
547570 filter: NSPredicate ? = nil ,
548571 keyPaths: [ String ] ? = nil ,
549572 sortDescriptor: SortDescriptor ? = nil ) where ResultType: Object {
550- self . storage = Storage ( Results ( RLMResults< ResultType> . emptyDetached( ) ) , keyPaths)
551- self . storage. configuration = configuration
552- self . filter = filter
553- self . sortDescriptor = sortDescriptor
573+ let results = Results< ResultType> ( RLMResults< ResultType> . emptyDetached( ) )
574+ self . storage = Storage ( results,
575+ configuration: configuration,
576+ filter: filter,
577+ sortDescriptor: sortDescriptor)
554578 }
555579#if swift(>=5.5)
556580 /**
@@ -571,20 +595,22 @@ extension Projection: _ObservedResultsValue { }
571595 where: ( ( Query < ResultType > ) -> Query < Bool > ) ? = nil ,
572596 keyPaths: [ String ] ? = nil ,
573597 sortDescriptor: SortDescriptor ? = nil ) where ResultType: Object {
574- self . storage = Storage ( Results ( RLMResults< ResultType> . emptyDetached( ) ) , keyPaths)
575- self . storage. configuration = configuration
576- self . where = `where`
577- self . sortDescriptor = sortDescriptor
598+ let results = Results< ResultType> ( RLMResults< ResultType> . emptyDetached( ) )
599+ self . storage = Storage ( results,
600+ configuration: configuration,
601+ where: `where`,
602+ sortDescriptor: sortDescriptor)
578603 }
579604#endif
580605 /// :nodoc:
581606 public init ( _ type: ResultType . Type ,
582607 keyPaths: [ String ] ? = nil ,
583608 configuration: Realm . Configuration ? = nil ,
584609 sortDescriptor: SortDescriptor ? = nil ) where ResultType: Object {
585- self . storage = Storage ( Results ( RLMResults< ResultType> . emptyDetached( ) ) , keyPaths)
586- self . storage. configuration = configuration
587- self . sortDescriptor = sortDescriptor
610+ let results = Results< ResultType> ( RLMResults< ResultType> . emptyDetached( ) )
611+ self . storage = Storage ( results,
612+ configuration: configuration,
613+ sortDescriptor: sortDescriptor)
588614 }
589615
590616 public mutating func update( ) {
0 commit comments