RLMListBase.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "RLMListBase.h"
  19. #import "RLMArray_Private.hpp"
  20. #import "RLMObjectSchema_Private.h"
  21. #import "RLMObject_Private.hpp"
  22. #import "RLMObservation.hpp"
  23. #import "RLMProperty_Private.h"
  24. #import "RLMRealm_Private.hpp"
  25. @interface RLMArray (KVO)
  26. - (NSArray *)objectsAtIndexes:(__unused NSIndexSet *)indexes;
  27. @end
  28. @implementation RLMListBase {
  29. std::unique_ptr<RLMObservationInfo> _observationInfo;
  30. }
  31. + (RLMArray *)_unmanagedArray {
  32. return nil;
  33. }
  34. - (instancetype)init {
  35. return self = [super init];
  36. }
  37. - (instancetype)initWithArray:(RLMArray *)array {
  38. self = [super init];
  39. if (self) {
  40. __rlmArray = array;
  41. }
  42. return self;
  43. }
  44. - (RLMArray *)_rlmArray {
  45. if (!__rlmArray) {
  46. __rlmArray = self.class._unmanagedArray;
  47. }
  48. return __rlmArray;
  49. }
  50. - (id)valueForKey:(NSString *)key {
  51. return [self._rlmArray valueForKey:key];
  52. }
  53. - (id)valueForKeyPath:(NSString *)keyPath {
  54. return [self._rlmArray valueForKeyPath:keyPath];
  55. }
  56. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  57. objects:(id __unsafe_unretained [])buffer
  58. count:(NSUInteger)len {
  59. return [self._rlmArray countByEnumeratingWithState:state objects:buffer count:len];
  60. }
  61. - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes {
  62. return [self._rlmArray objectsAtIndexes:indexes];
  63. }
  64. - (void)addObserver:(id)observer
  65. forKeyPath:(NSString *)keyPath
  66. options:(NSKeyValueObservingOptions)options
  67. context:(void *)context {
  68. RLMEnsureArrayObservationInfo(_observationInfo, keyPath, self._rlmArray, self);
  69. [super addObserver:observer forKeyPath:keyPath options:options context:context];
  70. }
  71. @end
  72. @implementation RLMLinkingObjectsHandle {
  73. realm::TableKey _tableKey;
  74. realm::ObjKey _objKey;
  75. RLMClassInfo *_info;
  76. RLMRealm *_realm;
  77. RLMProperty *_property;
  78. RLMResults *_results;
  79. }
  80. - (instancetype)initWithObject:(RLMObjectBase *)object property:(RLMProperty *)prop {
  81. if (!(self = [super init])) {
  82. return nil;
  83. }
  84. auto& obj = object->_row;
  85. _tableKey = obj.get_table()->get_key();
  86. _objKey = obj.get_key();
  87. _info = object->_info;
  88. _realm = object->_realm;
  89. _property = prop;
  90. return self;
  91. }
  92. - (instancetype)initWithLinkingObjects:(RLMResults *)linkingObjects {
  93. if (!(self = [super init])) {
  94. return nil;
  95. }
  96. _realm = linkingObjects.realm;
  97. _results = linkingObjects;
  98. return self;
  99. }
  100. - (instancetype)freeze {
  101. RLMLinkingObjectsHandle *frozen = [[self.class alloc] init];
  102. frozen->_results = [self.results freeze];
  103. return frozen;
  104. }
  105. - (RLMResults *)results {
  106. if (_results) {
  107. return _results;
  108. }
  109. [_realm verifyThread];
  110. auto table = _realm.group.get_table(_tableKey);
  111. if (!table->is_valid(_objKey)) {
  112. @throw RLMException(@"Object has been deleted or invalidated.");
  113. }
  114. auto obj = _realm.group.get_table(_tableKey)->get_object(_objKey);
  115. auto& objectInfo = _realm->_info[_property.objectClassName];
  116. auto& linkOrigin = _info->objectSchema->computed_properties[_property.index].link_origin_property_name;
  117. auto linkingProperty = objectInfo.objectSchema->property_for_name(linkOrigin);
  118. realm::Results results(_realm->_realm, obj.get_backlink_view(objectInfo.table(), linkingProperty->column_key));
  119. _results = [RLMLinkingObjects resultsWithObjectInfo:objectInfo results:std::move(results)];
  120. _realm = nil;
  121. return _results;
  122. }
  123. @end