Skip to content

Injection

Michael Seghers edited this page Aug 1, 2016 · 3 revisions

Injection

All objects created in an object context will be injected after their creation. Reliant will scan your object's properties. If a writable property's name matches with a key or an alias for an object in the object context, and if its current value is nil, the matching object will be injected in this property. All other properties will be left alone. This will be done for the entire class hierarchy of the instance.

Injecting objects that are not know to the object context

You can use the injection mechanism described above on objects which are not setup in the object context. A good example would be a UIViewController. Reliant has a category on NSObject which enables you to inject any object, as long as it can find a context.

Reliant will react on memory warnings by removing any object it still holds in its scopes. Reliant thereby releases its ownership of the instances. However, it can not be held responsible for the objects injected outside of its scope as discussed above. You should therefore keep strong references on any injected object yourself in most cases.

This is what you need to do:

//In your UIViewController
@implementation MyViewController ()
@property (nonatomic, strong) id<Foo> foo;
@end

@implementation MyViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    
    [self ocsInject];
}

@end

And that's all there is to it. The property foo will be injected by Reliant.

Preventing injection on specific properties

Reliant will, by default, not try to inject properties like view on UIViewController. If your implementation has a need for ignoring properties you can implement the following method in your class:

+ (BOOL) ocsReliantShouldIgnorePropertyWithName:(NSString *) name {
    static NSArray *excludedProps;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        excludedProps = @[@"ignoredProperty1", @"ignoredProperty2"];
    });
    
    //it's important here to call this method on super, as Reliant has it's own category on NSObject
    return [super ocsReliantShouldIgnorePropertyWithName:name] || [excludedProps containsObject:name];
}

For your information here are the default ignored properties on the classes they belong to:

  • NSObject
    • accessibilityLanguage
    • accessibilityValue
    • accessibilityHint
    • accessibilityLabel
    • accessibilityPath
  • UIResponder
    • restorationIdentifier
  • UIViewController
    • tabBarItem
    • title
    • toolbarItems
    • view
    • aggregateStatisticsDisplayCountKey
    • nibName
    • storyboard
    • parentViewController
    • modalTransitionView
    • mutableChildViewControllers
    • childModalViewController
    • parentModalViewController
    • searchDisplayController
    • dropShadowView
    • afterAppearanceBlock
    • transitioningDelegate
    • customTransitioningView