johnno1962 / InjectionIII

Re-write of Injection for Xcode in (mostly) Swift
MIT License
4.07k stars 319 forks source link

Why can't I use it with the RAC (ReactiveObjC) framework? #124

Closed snlo closed 5 years ago

snlo commented 5 years ago

Xcode10.2 InjectionIII v.1.51 #import <ReactiveObjC.h> `- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.

// add subviews
[self configureUserInterface];

// add dataSource configure
[self configureDataSource];

}

} - (void)injected { self.view.backgroundColor = [UIColor blackColor]; [self.buttonSaveCodeImage setTitle:@"😝" forState:UIControlStateNormal];

[[self.buttonSaveCodeImage rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    NSLog(@" - -- 111 - - ");

}];

} Thread 1: EXC_BAD_ACCESS (code=1, address=0x4f915cf38d20) πŸ’‰ Compiling /Users/snlo/Desktop/Demo/QQQ/QQQ/ajeivnweqornviw/OpenShareDistributionPromotionViewController.m πŸ’‰ Loading .dylib ... objc[6372]: Class OpenShareDistributionPromotionViewController is implemented in both /Users/snlo/Library/Developer/CoreSimulator/Devices/E9DBA0FA-06D3-4F61-840D-A814F163F387/data/Containers/Bundle/Application/E9618335-339E-4AFB-8461-63864B9CD143/QQQ.app/QQQ (0x1048f6780) and /Users/snlo/Library/Containers/com.johnholdsworth.InjectionIII/Data/eval102.dylib (0x12c3ff5c8). One of the two will be used. Which one is undefined. πŸ’‰ Loaded .dylib - Ignore any duplicate class warning ^ 2019-04-01 17:05:28.024 QQQ[6372:4243698] hello world (lldb) `

johnno1962 commented 5 years ago

Hi, last time I looked RAC uses __unsafe pointers which doesn’t work well with the β€œsweep” that implements the -(void)injected method. You’ll have to try using the INJECTION_BUNDLE_NOTIFICATION notification which is sent each time you inject instead.

snlo commented 5 years ago

Subscribing to the INJECTION_BUNDLE_NOTIFICATION notification is indeed valid. There is also a workaround in which the previously subscribed signals are cleaned up in the function injected because they appear to be injected with duplicate subscriptions.

`- (void)injected { NSLog(@" - - %s",func);

[self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([obj isKindOfClass:UIControl.class]) {
        UIControl * control = (UIControl *)obj;
        NSLog(@" -- %@",control.allTargets);
        [control.allTargets enumerateObjectsUsingBlock:^(id  _Nonnull objx, BOOL * _Nonnull stop) {
            [control removeTarget:objx action:@selector(sendNext:) forControlEvents:UIControlEventTouchUpInside];
        }];
    }
}];

[[self.buttonTest rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    NSLog(@"--injected ok--");
    self.labelTest.text = @"button test";
}];

}`