Обратный звонок сильный ссылочный цикл - PullRequest
0 голосов
/ 27 апреля 2018

Следующее создает сильный референсный цикл? У меня такое ощущение, что я ссылаюсь на 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 голосов
/ 27 апреля 2018

Вы можете использовать слабую переменную self:

- (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];
        });
    }];
}
...