【问题标题】:Callback strong Reference Cycle回调强引用循环
【发布时间】:2018-04-27 01:33:18
【问题描述】:

以下是否创建了一个强引用循环?我有一种感觉是因为我在回调中引用了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];
        });
    }];
}

【问题讨论】:

    标签: objective-c asynchronous reference-cycle


    【解决方案1】:

    你可以使用 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];
            });
        }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多