RLMListBase.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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::Row _row;
  74. RLMClassInfo *_info;
  75. RLMRealm *_realm;
  76. RLMProperty *_property;
  77. RLMResults *_results;
  78. }
  79. - (instancetype)initWithObject:(RLMObjectBase *)object property:(RLMProperty *)prop {
  80. if (!(self = [super init])) {
  81. return nil;
  82. }
  83. _row = object->_row;
  84. _info = object->_info;
  85. _realm = object->_realm;
  86. _property = prop;
  87. return self;
  88. }
  89. - (RLMResults *)results {
  90. if (_results) {
  91. return _results;
  92. }
  93. if (!_row.is_attached()) {
  94. @throw RLMException(@"Object has been deleted or invalidated.");
  95. }
  96. [_realm verifyThread];
  97. auto& objectInfo = _realm->_info[_property.objectClassName];
  98. auto& linkOrigin = _info->objectSchema->computed_properties[_property.index].link_origin_property_name;
  99. auto linkingProperty = objectInfo.objectSchema->property_for_name(linkOrigin);
  100. auto backlinkView = _row.get_table()->get_backlink_view(_row.get_index(),
  101. objectInfo.table(),
  102. linkingProperty->table_column);
  103. realm::Results results(_realm->_realm, std::move(backlinkView));
  104. _results = [RLMLinkingObjects resultsWithObjectInfo:objectInfo results:std::move(results)];
  105. _realm = nil;
  106. return _results;
  107. }
  108. - (RLMObjectBase *)parent {
  109. RLMObjectBase *obj = RLMCreateManagedAccessor(_info->rlmObjectSchema.accessorClass, _info);
  110. obj->_row = _row;
  111. return obj;
  112. }
  113. @end