@@ -207,7 +207,7 @@ private final class ObservableStoragePublisher<ObjectType>: Publisher where Obje
207
207
if value. realm != nil && !value. isInvalidated, let value = value. thaw ( ) {
208
208
// This path is for cases where the object is already managed. If an
209
209
// unmanaged object becomes managed it will continue to use KVO.
210
- let token = value. _observe ( keyPaths, subscriber)
210
+ let token = value. _observe ( keyPaths, subscriber)
211
211
subscriber. receive ( subscription: ObservationSubscription ( token: token) )
212
212
} else if let value = unwrappedValue, !value. isInvalidated {
213
213
// else if the value is unmanaged
@@ -222,6 +222,9 @@ private final class ObservableStoragePublisher<ObjectType>: Publisher where Obje
222
222
let subscription = SwiftUIKVO . Subscription ( observer: kvo, value: value, keyPaths: keyPaths)
223
223
subscriber. receive ( subscription: subscription)
224
224
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 ( ) )
225
228
}
226
229
}
227
230
}
@@ -231,10 +234,8 @@ private class ObservableStorage<ObservedType>: ObservableObject where ObservedTy
231
234
@Published var value : ObservedType {
232
235
willSet {
233
236
if newValue != value {
234
- objectWillChange. subscribers. forEach {
235
- $0. receive ( subscription: ObservationSubscription ( token: newValue. _observe ( keyPaths, $0) ) )
236
- }
237
237
objectWillChange. send ( )
238
+ self . objectWillChange = ObservableStoragePublisher ( newValue, keyPaths)
238
239
}
239
240
}
240
241
}
@@ -405,14 +406,12 @@ extension Projection: _ObservedResultsValue { }
405
406
///
406
407
/// Given `@ObservedResults var v` in SwiftUI, `$v` refers to a `BoundCollection`.
407
408
///
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 , * )
409
410
@propertyWrapper public struct ObservedResults < ResultType> : DynamicProperty , BoundCollection where ResultType: _ObservedResultsValue & RealmFetchable & KeypathSortable & Identifiable {
410
411
private class Storage : ObservableStorage < Results < ResultType > > {
411
412
var setupHasRun = false
412
413
private func didSet( ) {
413
- if setupHasRun {
414
- setupValue ( )
415
- }
414
+ setupValue ( )
416
415
}
417
416
418
417
func setupValue( ) {
@@ -453,6 +452,30 @@ extension Projection: _ObservedResultsValue { }
453
452
}
454
453
455
454
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
+ }
456
479
}
457
480
458
481
@Environment ( \. realmConfiguration) var configuration
@@ -524,10 +547,10 @@ extension Projection: _ObservedResultsValue { }
524
547
keyPaths: [ String ] ? = nil ,
525
548
sortDescriptor: SortDescriptor ? = nil ) where ResultType: Projection < ObjectType > , ObjectType: ThreadConfined {
526
549
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 )
531
554
}
532
555
/**
533
556
Initialize a `ObservedResults` struct for a given `Object` or `EmbeddedObject` type.
@@ -547,10 +570,11 @@ extension Projection: _ObservedResultsValue { }
547
570
filter: NSPredicate ? = nil ,
548
571
keyPaths: [ String ] ? = nil ,
549
572
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)
554
578
}
555
579
#if swift(>=5.5)
556
580
/**
@@ -571,20 +595,22 @@ extension Projection: _ObservedResultsValue { }
571
595
where: ( ( Query < ResultType > ) -> Query < Bool > ) ? = nil ,
572
596
keyPaths: [ String ] ? = nil ,
573
597
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)
578
603
}
579
604
#endif
580
605
/// :nodoc:
581
606
public init ( _ type: ResultType . Type ,
582
607
keyPaths: [ String ] ? = nil ,
583
608
configuration: Realm . Configuration ? = nil ,
584
609
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)
588
614
}
589
615
590
616
public mutating func update( ) {
0 commit comments