Ios 在操作过程中取消分配委托

Ios 在操作过程中取消分配委托,ios,memory-management,delegates,nsoperation,dealloc,Ios,Memory Management,Delegates,Nsoperation,Dealloc,我正在寻找解决以下问题的解决方案: 我有一个在后台下载图像的操作: @protocol CoreImageDownloadingOperationDelegate <NSObject> @required -(void) handleResponse:(UIImage*) response; -(void) handleException:(MobileServiceException*) exception; @end @interface CoreImageDownload

我正在寻找解决以下问题的解决方案:

我有一个在后台下载图像的操作:

@protocol CoreImageDownloadingOperationDelegate <NSObject>

@required
-(void) handleResponse:(UIImage*) response;
-(void) handleException:(MobileServiceException*) exception;

@end

@interface CoreImageDownloadingOperation : NSOperation{
}

-(id) initWithDelegate:(id<CoreImageDownloadingOperationDelegate>)del andImageID: (NSString *) image;

@property (nonatomic, assign) id <CoreImageDownloadingOperationDelegate> delegate;
问题是:当我在图像下载时转到另一个页面时,cachedImageView被解除分配,但是当imageDownloadingOperation完成下载时,委托不是零,它正在尝试处理响应。。。当然,我会收到一条信息发送到deallocated

我在CachedImageView中按如下方式分配初始化操作:

CoreImageDownloadingOperation* imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];
或:


编写代码的更好方法是:

 if([self.delegate respondsToSelector:@selector(handleResponse:)){
    [self.delegate handleResponse:image];
}

这将避免崩溃。

协议声明在哪里?我希望看到:

@protocol CoreImageDownloadingOperationDelegate <NSObject> 

- (void) handleResponse:(UIImage *) image;

@end

@interface CoreImageDownloadingOperation : NSOperation{
}

-(id) initWithDelegate:(id<CoreImageDownloadingOperationDelegate>)del andImageID: (NSString *) image;

@property (nonatomic, assign) id <CoreImageDownloadingOperationDelegate> delegate;
您不需要检查
if(self.delegate&&[self.delegate responses….
,因为如果委托为nil,它将返回nil&如果没有实现选择器

编辑*

您在其中创建:

CoreImageDownloadingOperation* imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];
我怀疑这是被释放的,请将其转换为它所在类的属性。然后重试(确保在完成后释放它),即

在你的

@property (nonatomic, retain) CoreImageDownloadingOperation* imageDownloadingOperation;
然后用以下命令初始化:

if (!self.imageDownloadingOperation)
    self.imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];

我已经解决了问题,我使用了CW0007007解决方案和我自己的解决方案。因此,我将我的操作转换为保留属性:

@property (nonatomic, retain) CoreImageDownloadingOperation* imageDownloadingOperation;
在此之后,我检查了操作是否仍然有效

if (!imageDownloadingOperation)
        imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];
然后添加到操作队列中

在dealloc中,将委托设置为nil(仅当操作处于活动状态时才为ofc),然后释放它:

if (imageDownloadingOperation) {
    imageDownloadingOperation.delegate = nil;
    [imageDownloadingOperation release];
}
在操作中:(现在如果imageView解除分配,其委托将为零,并且不会在任何时候崩溃)

问题是:当我转到另一个页面时,图像是 下载时,将释放cachedImageView

处理这种情况的通常方法是在CachedImageView的dealloc中将自身作为委托删除

// in CachedImageView
- (void)dealloc {
  // CachedImageView keeps a reference to the operation
  // called imageDownloadingOperation
  imageDownloadingOperation.delegate = nil;
  [super dealloc];
}

用weakI替换assign属性我不使用ARC,所以我不能使用Weakt有很多框架可以异步加载图像。例如,SDWebImage。弱和assign是相同的,对ARC项目使用弱,但它们是相同的。您的协议声明在哪里?我用协议声明更新了我的问题这没有解决我的问题。它在respondsToSelector崩溃。请参见上面的图片。请参见上面的帖子。您必须首先在dealloc方法中将委托设置为nil。否则,无论您正在检查什么,应用程序都将崩溃。我正在这样做。问题是在图像视图取消分配后,委托仍然不是nil,并且操作失败尝试调用HandlerResponse方法时,我收到一条发送到DealLocated的消息,这不是它崩溃的原因,如果代理不是nil,但没有响应选择器,则它不会输入if。还有其他原因。我用更新了代码。但如果imageView是DealLocat,则仍然会崩溃,并且图像没有下载。我升级了谢谢,这对我很有帮助。这基本上就是我说的,如果我的回答对你有帮助,请记下我的回答是正确的。
if (!imageDownloadingOperation)
        imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];
if (imageDownloadingOperation) {
    imageDownloadingOperation.delegate = nil;
    [imageDownloadingOperation release];
}
if (delegate)        
    if ([delegate respondsToSelector:@selector(handleResponse:)])
        [delegate handleResponse:image];
// in CachedImageView
- (void)dealloc {
  // CachedImageView keeps a reference to the operation
  // called imageDownloadingOperation
  imageDownloadingOperation.delegate = nil;
  [super dealloc];
}