iOS-Blöcke und starke / schwache Referenzen auf sich selbst

Ich habe eine Frage über starke und schwache Referenzen, um sich selbst in Blöcken in iOS. Ich weiß, den richtigen Weg zu finden, um sich selbst in einen block zu schaffen, ist ein schwacher Verweis außerhalb des Blocks, und dann einen starken Hinweis auf, dass die schwache Referenz innerhalb des Blocks, so wie hier:

__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
    typeof(self) strongSelf = weakSelf;
    NSLog(@"%@", strongSelf.someProperty);
});

Jedoch, was passiert, wenn Sie verschachtelte Blöcke? Ist die eine Reihe von Referenzen genug? Oder benötigen Sie einen neuen Satz für jeden block? Zum Beispiel, welche der folgenden ist richtig?

:

__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
    typeof(self) strongSelf = weakSelf;
    NSLog(@"%@", strongSelf.someProperty);
    dispatch_async(dispatch_get_main_queue(), ^ {
        strongSelf.view.frame = CGRectZero;
    });
});

Oder so:

__weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
        typeof(self) strongSelf = weakSelf;
        NSLog(@"%@", strongSelf.someProperty);
        __weak typeof(strongSelf) weakSelf1 = strongSelf;
        dispatch_async(dispatch_get_main_queue(), ^ {
            typeof(strongSelf) strongSelf1 = weakSelf1;
            strongSelf1.view.frame = CGRectZero;
        });
    });

Jede information oder Erklärung sehr dankbar!

InformationsquelleAutor der Frage Mason | 2013-09-26

Schreibe einen Kommentar