RLMObservation.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Foundation/Foundation.h>
  19. #import "binding_context.hpp"
  20. #import <realm/row.hpp>
  21. #import <realm/table.hpp>
  22. #import <unordered_map>
  23. @class RLMObjectBase, RLMRealm, RLMSchema, RLMProperty, RLMObjectSchema;
  24. class RLMClassInfo;
  25. class RLMSchemaInfo;
  26. namespace realm {
  27. class History;
  28. class SharedGroup;
  29. }
  30. // RLMObservationInfo stores all of the KVO-related data for RLMObjectBase and
  31. // RLMArray. There is a one-to-one relationship between observed objects and
  32. // RLMObservationInfo instances, so it could be folded into RLMObjectBase, and
  33. // is a separate class mostly to avoid making all accessor objects far larger.
  34. //
  35. // RLMClassInfo stores a vector of pointers to the first observation info
  36. // created for each row. If there are multiple observation infos for a single
  37. // row (such as if there are multiple observed objects backed by a single row,
  38. // or if both an object and an array property of that object are observed),
  39. // they're stored in an intrusive doubly-linked-list in the `next` and `prev`
  40. // members. This is done primarily to make it simpler and faster to loop over
  41. // all of the observed objects for a single row, as that needs to be done for
  42. // every change.
  43. class RLMObservationInfo {
  44. public:
  45. RLMObservationInfo(id object);
  46. RLMObservationInfo(RLMClassInfo &objectSchema, std::size_t row, id object);
  47. ~RLMObservationInfo();
  48. realm::Row const& getRow() const {
  49. return row;
  50. }
  51. NSString *columnName(size_t col) const noexcept;
  52. // Send willChange/didChange notifications to all observers for this object/row
  53. // Sends the array versions if indexes is non-nil, normal versions otherwise
  54. void willChange(NSString *key, NSKeyValueChange kind=NSKeyValueChangeSetting, NSIndexSet *indexes=nil) const;
  55. void didChange(NSString *key, NSKeyValueChange kind=NSKeyValueChangeSetting, NSIndexSet *indexes=nil) const;
  56. bool isForRow(size_t ndx) const {
  57. return row && row.get_index() == ndx;
  58. }
  59. void recordObserver(realm::Row& row, RLMClassInfo *objectInfo, RLMObjectSchema *objectSchema, NSString *keyPath);
  60. void removeObserver();
  61. bool hasObservers() const { return observerCount > 0; }
  62. // valueForKey: on observed object and array properties needs to return the
  63. // same object each time for KVO to work at all. Doing this all the time
  64. // requires some odd semantics to avoid reference cycles, so instead we do
  65. // it only to the extent specifically required by KVO. In addition, we
  66. // need to continue to return the same object even if this row is deleted,
  67. // or deleting an object with active observers will explode horribly.
  68. // Once prepareForInvalidation() is called, valueForKey() will always return
  69. // the cached value for object and array properties without checking the
  70. // backing row to verify it's up-to-date.
  71. //
  72. // prepareForInvalidation() must be called on the head of the linked list
  73. // (i.e. on the object pointed to directly by the object schema)
  74. id valueForKey(NSString *key);
  75. void prepareForInvalidation();
  76. private:
  77. // Doubly-linked-list of observed objects for the same row as this
  78. RLMObservationInfo *next = nullptr;
  79. RLMObservationInfo *prev = nullptr;
  80. // Row being observed
  81. realm::Row row;
  82. RLMClassInfo *objectSchema = nullptr;
  83. // Object doing the observing
  84. __unsafe_unretained id object = nil;
  85. // valueForKey: hack
  86. bool invalidated = false;
  87. size_t observerCount = 0;
  88. NSString *lastKey = nil;
  89. __unsafe_unretained RLMProperty *lastProp = nil;
  90. // objects returned from valueForKey() to keep them alive in case observers
  91. // are added and so that they can still be accessed after row is detached
  92. NSMutableDictionary *cachedObjects;
  93. void setRow(realm::Table &table, size_t newRow);
  94. template<typename F>
  95. void forEach(F&& f) const {
  96. // The user's observation handler may release their last reference to
  97. // the object being observed, which will result in the RLMObservationInfo
  98. // being destroyed. As a result, we need to retain the object which owns
  99. // both `this` and the current info we're looking at.
  100. __attribute__((objc_precise_lifetime)) id self = object, current;
  101. for (auto info = prev; info; info = info->prev) {
  102. current = info->object;
  103. f(info->object);
  104. }
  105. for (auto info = this; info; info = info->next) {
  106. current = info->object;
  107. f(info->object);
  108. }
  109. }
  110. // Default move/copy constructors don't work due to the intrusive linked
  111. // list and we don't need them
  112. RLMObservationInfo(RLMObservationInfo const&) = delete;
  113. RLMObservationInfo(RLMObservationInfo&&) = delete;
  114. RLMObservationInfo& operator=(RLMObservationInfo const&) = delete;
  115. RLMObservationInfo& operator=(RLMObservationInfo&&) = delete;
  116. };
  117. // Get the the observation info chain for the given row
  118. // Will simply return info if it's non-null, and will search ojectSchema's array
  119. // for a matching one otherwise, and return null if there are none
  120. RLMObservationInfo *RLMGetObservationInfo(RLMObservationInfo *info, size_t row, RLMClassInfo& objectSchema);
  121. // delete all objects from a single table with change notifications
  122. void RLMClearTable(RLMClassInfo &realm);
  123. // invoke the block, sending notifications for cascading deletes/link nullifications
  124. void RLMTrackDeletions(RLMRealm *realm, dispatch_block_t block);
  125. std::vector<realm::BindingContext::ObserverState> RLMGetObservedRows(RLMSchemaInfo const& schema);
  126. void RLMWillChange(std::vector<realm::BindingContext::ObserverState> const& observed, std::vector<void *> const& invalidated);
  127. void RLMDidChange(std::vector<realm::BindingContext::ObserverState> const& observed, std::vector<void *> const& invalidated);