我们如何以编程方式检查 Obj-C 对象是否不再存在(已被收集)

发布于 2024-10-22 05:04:49 字数 1163 浏览 1 评论 0原文

我创建了一个回调类,它充当轻量级可重用委托模型的对象+选择器持有者。代码如下:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-10-29 05:04:49

您无法检查您的 Callback 对象是否已被收集。这个概念在垃圾收集中没有意义——只有当你没有剩余的方法来访问该对象时,该对象才会被收集。您可以__weak限定符放在实例变量上,这意味着一旦对象被收集,变量就会变成nil。因此,请检查引用是否为零,而不是对象本身是否已被收集。

如果需要弱引用对象的集合,则需要使用 NSPointerArrayNSHashTableNSHashMap

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 become nil. 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, or NSHashMap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文