简体   繁体   English

iOS检查调用方法之前是否存在委托

[英]iOS check if delegate exists before call method

I write iOS app and use imageStore library to lazy load images and cache them in memory. 我编写iOS应用程序并使用imageStore库来延迟加载图像并将它们缓存在内存中。 (https://github.com/psychs/imagestore) (https://github.com/psychs/imagestore)

On ViewController I create imagestore instance: 在ViewController上我创建了imagestore实例:

imageStore = [ImageStore new];
imageStore.delegate = self;

When image loaded successfuly, imagestore call delegate method 当图像加载成功时,imagestore调用委托方法

- (void)imageStoreDidGetNewImage:(ImageStore*)sender url:(NSString*)url

that doing reloadData on tableview to redraw cells. 在tableview上执行reloadData以重绘单元格。 All works good. 一切都很好。 But there is the problem: if ViewController didUnload (go back in navigation controller) and image loaded, application finish with crash, because imagestore call method of unloaded ViewController. 但是有问题:如果ViewController执行了卸载(返回导航控制器)并加载了图像,应用程序完成崩溃,因为imagestore调用了卸载ViewController的方法。

I try to do following: 1) in ViewController I place this code in viewDidUnload section: 我尝试执行以下操作:1)在ViewController中,我将此代码放在viewDidUnload部分中:

imageStore.delegate = nil;
imageStore = nil;

2) In imageStore I added checking for nil: 2)在imageStore中我添加了检查nil:

if(delegate != nil) {
  ...call delegate method
}

It works, but periodically app crash anyway. 它可以工作,但无论如何都会定期崩溃。

Try putting this code on dealloc section. 尝试将此代码放在dealloc部分。

imageStore.delegate = nil;
imageStore = nil;

In the same way the if clause is not necessary because any call to an nil object is ignored by the application, so if you have something like this: 同样,if子句不是必需的,因为应用程序会忽略对nil对象的任何调用,所以如果你有这样的东西:

id delegate = nil;    
[delegate callAnyMethod];

has no effect in your application behavior, in other hand if the call of the method delegate is optional you should asure that delegate responds to selector, something like this should do the trick: 对你的应用程序行为没有任何影响,另一方面,如果方法委托的调用是可选的,你应该确保委托响应选择器,这样的事情应该做的伎俩:

if([delegate conformsToProtocol:@protocol(yourProtocolName)] && [delegate respondsToSelector:@selector(imageStoreDidGetNewImage:url:)]) {
       [delegate imageStoreDidGetNewImage:imageStore url:url];
}

Cheers! 干杯!

It works, but periodically app crash anyway. 它可以工作,但无论如何都会定期崩溃。

That's a contradiction. 这是一个矛盾。 There are two possibilities: 有两种可能性:

  1. Your fix worked, and the app is crashing for some other reason. 您的修复工作正常,应用程序因其他原因而崩溃。

  2. Your fix did not work, the app continues to crash for the same reason it was crashing before. 您的修复程序无法正常工作,应用程序继续崩溃的原因与以前崩溃的原因相同。

It's hard to know what's wrong without knowing which of these two possibilities is in fact happening. 如果不知道这两种可能性中的哪一种实际上正在发生,很难知道什么是错的。 Look at the error message and the evidence from the crash, such as the stack crawl. 查看错误消息和崩溃中的证据,例如堆栈爬网。 Why is the app crashing? 为什么应用程序崩溃了? Does it try to dereference the delegate property somewhere without checking it first? 它是否尝试取消引用某个delegate属性而不先检查它? Does it depend on the delegate doing something, so that if the delegate no longer exists that thing doesn't get done and that in turn leads to a crash? 它是否依赖于委托做某事,所以如果委托不再存在那件事就不会完成而反过来会导致崩溃? These are the kinds of things I'd look for, but again the most important thing is to start with the evidence you have and follow your nose. 这些是我要寻找的东西,但最重要的是从你拥有的证据开始并遵循你的鼻子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM