Ich habe diesen Code und was ich versuche zu tun ist, sich selbst lebendig in dem Block zu halten, dass es auf dem Hauptthread ausgeführt wird. Das Ergebnis ist zufällig und druckt manchmal null.iOS - GCD und __starke Referenz
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.proArray = [[NSMutableArray alloc]init];
GCDVC2* __weak weakSelf = self;
self.postGCDBlock = ^{
GCDVC2* __strong strongSelf2 = weakSelf;
[strongSelf2.proArray removeObject:@"3"];
NSLog(@"%@",strongSelf2.proArray);
[strongSelf2.activityIndicator stopAnimating];
};
self.addObjectsBlock = ^{
GCDVC2* __strong strongSelf = weakSelf;
[strongSelf.proArray addObject:@"2"];
[strongSelf.proArray addObject:@"3"];
[NSThread sleepForTimeInterval:5];
dispatch_async(dispatch_get_main_queue(),strongSelf.postGCDBlock);
};
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), self.addObjectsBlock);
}
Dieser Code funktioniert:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.proArray = [[NSMutableArray alloc]init];
GCDVC2* __weak weakSelf = self;
//self.postGCDBlock = ;
self.addObjectsBlock = ^{
GCDVC2* __strong strongSelf = weakSelf;
[strongSelf.proArray addObject:@"2"];
[strongSelf.proArray addObject:@"3"];
[NSThread sleepForTimeInterval:5];
GCDVC2* __weak weakSelf2 = strongSelf;
dispatch_async(dispatch_get_main_queue(),^{
GCDVC2* __strong strongSelf = weakSelf2;
[strongSelf.proArray removeObject:@"3"];
NSLog(@"%@",strongSelf.proArray);
[strongSelf.activityIndicator stopAnimating];
});
};
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), self.addObjectsBlock);
}
Gibt es eine Möglichkeit, das zweite Stück Code zu konvertieren mit dem Aufbau des ersten Stückes Code zu arbeiten? Ich habe viele Variationen ausprobiert, aber es ist immer zufällig. Kann ich irgendwie versichern, dass der self.postGCDBlock kein self to nil haben wird?
Update: Eigenschaftsdeklaration:
typedef void(^CustomBlock)(void);
@interface GCDVC2()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property(nonatomic,strong)NSMutableArray *proArray;
@property (nonatomic, copy) CustomBlock addObjectsBlock;
@property (nonatomic, copy) CustomBlock postGCDBlock;
@end
Wie sieht die Eigenschaftsdeklaration im View Controller aus? –
Die Antwort wurde aktualisiert! – BlackM