回调强参考周期

时间:2018-04-27 01:33:19

标签: objective-c asynchronous reference-cycle

以下是否创建了强大的参考周期?我有一种感觉,因为我在回调中引用了self

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [self cropImageWithInfo:info];
        if (currentScreen == CurrentScreenApartment) {
            [self.profileViewModel.apartmentPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
        }
        else {
            [self.profileViewModel.userPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self reloadPhotosCell];
        });
    }];
}

1 个答案:

答案 0 :(得分:0)

您可以使用自我的弱变量:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    typeof(self) __weak weakSelf = self;

    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [weakSelf cropImageWithInfo:info];
        if (currentScreen == CurrentScreenApartment) {
            [weakSelf.profileViewModel.apartmentPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
        }
        else {
            [weakSelf.profileViewModel.userPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf reloadPhotosCell];
        });
    }];
}