我们如何以编程方式检查 Obj-C 对象是否不再存在(已被收集)
我创建了一个回调类,它充当轻量级可重用委托模型的对象+选择器持有者。代码如下:
#import <Foundation/Foundation.h>
@interface Callback : NSObject {
id obj;
SEL method;
}
@property(nonatomic,assign) id obj;
@property(nonatomic,assign) SEL method;
+(Callback*) new:(id) obj1 :(SEL)method1;
@end
.m...的
#import "Callback.h"
@implementation Callback
@synthesize obj;
@synthesize method;
+(Callback*) new:(id) obj1 :(SEL)method1{
Callback * cb = [[Callback alloc] init];
cb.obj= obj1;
cb.method= method1;
return [cb autorelease];
}
@end
想法是我可以只传递 [Callback new:self :@selector(mymethod:)],甚至是回调的 NSArray,而不是重量级协议实现。下面是它的实际示例:
-(void) storeInCache:(NSArray*) contacts{
if (contacts != nil){
// TODO
}
}
- (void)viewDidLoad{
[self.service request_getContacts:[Callback new:self :@selector(storeInCache:)]];
// can fire off more requests on the common service class here
// as long as I give separate Callback method references
}
我使用它来传递到我的 HTTP 服务类,该类执行一大堆异步方法请求。现在,我的服务类保留了对异步方法返回时必须调用的回调对象的弱引用(分配)。那时我如何检查“回调”对象现在是否是僵尸?我的一般方法很糟糕吗?
I have made a Callback class which serves as an object+selector holder for a lightweight reusable delegate model. Here's the code:
#import <Foundation/Foundation.h>
@interface Callback : NSObject {
id obj;
SEL method;
}
@property(nonatomic,assign) id obj;
@property(nonatomic,assign) SEL method;
+(Callback*) new:(id) obj1 :(SEL)method1;
@end
And the .m...
#import "Callback.h"
@implementation Callback
@synthesize obj;
@synthesize method;
+(Callback*) new:(id) obj1 :(SEL)method1{
Callback * cb = [[Callback alloc] init];
cb.obj= obj1;
cb.method= method1;
return [cb autorelease];
}
@end
The idea is I can just pass in a [Callback new:self :@selector(mymethod:)], or even an NSArray of Callbacks instead of a heavy-weight protocol implementation. Here is an example of it in action:
-(void) storeInCache:(NSArray*) contacts{
if (contacts != nil){
// TODO
}
}
- (void)viewDidLoad{
[self.service request_getContacts:[Callback new:self :@selector(storeInCache:)]];
// can fire off more requests on the common service class here
// as long as I give separate Callback method references
}
I'm using it to pass through to my HTTP Service class that does a whole bunch of async method requests. Now my service class keeps a weak reference (assign) to the Callback obj it has to call when the async method returns. How can I check at that time whether the 'Callback' obj is now a zombie or not? Is my general approach terrible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法检查您的
Callback
对象是否已被收集。这个概念在垃圾收集中没有意义——只有当你没有剩余的方法来访问该对象时,该对象才会被收集。您可以将__weak
限定符放在实例变量上,这意味着一旦对象被收集,变量就会变成nil
。因此,请检查引用是否为零,而不是对象本身是否已被收集。如果需要弱引用对象的集合,则需要使用
NSPointerArray
、NSHashTable
或NSHashMap
。You can't check whether your
Callback
object has been collected. That concept doesn't make sense under garbage collection—the object won't be collected until you have no remaining ways to access the object. You can put the__weak
qualifier on your instance variable, which means that as soon as the object has been collected the variable will becomenil
. So check for the reference being nil, not for the object itself having been collected.If you need a collection of weakly-referenced objects, you need to use
NSPointerArray
,NSHashTable
, orNSHashMap
.