0

我正在尝试实现类似于

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

NSURLConnection,但我对内存管理有点困惑(我目前没有使用 ARC)。

我当前的代码是这样的:

@interface StuffInfoDownloader() <UIAlertViewDelegate>

typedef void (^StuffInfoDownloaderCompletionBlock)(NSArray *stuffs);

- (id)initStuffsWithIdentifiers:(NSSet *)identifiers
         completionHandler:(void (^)(NSArray *stuffs))handler;

@property (retain, nonatomic) StuffInfoDownloaderCompletionBlock completionHandler;
@property (retain, nonatomic) NSSet *identifiers;

@end

@implementation StuffInfoDownloader

@synthesize completionHandler = _completionHandler;
@synthesize identifiers = _identifiers;

+ (void)loadAsynchronouslyWithIdentifiers:(NSSet *)identifiers
                    completionHandler:(void (^)(NSArray *stuffs))handler
{
    StuffInfoDownloader *downloader = [[StuffInfoDownloader alloc] initStuffsWithIdentifiers:identifiers completionHandler:handler];

    [downloader downloadStuffs];
    [downloader release]; // will retain itself
}

- (id)initStuffsWithIdentifiers:(NSSet *)identifiers
          completionHandler:(void (^)(NSArray *stuffs))handler
{

    if (!(self = [super init])) {
        return nil;
    }

    [self retain];

    _completionHandler = handler;
    _identifiers = identifiers;

    return self;
}

- (void)downloadStuffs
{
    __block StuffInfoDownloader *me = self; // avoid reference cycle between self and the block
    [StuffsConnection loadAsynchronouslyWithIdentifiers:self.identifiers completionHandler:
    ^(NSArray *stuffs, NSError *error) {
         if(error) {
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed."
                                                         message:@"TODO do localised string"
                                                        delegate:self cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil, nil];
             [alert show];
             [alert release];
         } else {
             me.completionHandler(stuffs);
             [self release];
         }
    }];
}

#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
#pragma unused(alertView, buttonIndex)
    // try again
    [self downloadStuffs];
}

- (void)dealloc
{
    [_completionHandler release];
    [_identifiers release];
    [super dealloc];
}

基本上,我将对象的所有权传递给它自己,并在处理程序中释放它。有什么问题吗?

4

2 回答 2

1

这段代码有很多问题。除了块属性需要是copy. 你不应该做[self retain];and (ps 你在错误情况下[self release];错过了 a )。[self release]这完全违反了内存管理规则。如果你做对了,它们是完全没有必要的。Cocoa 中的内存管理完全是本地的——一个函数或方法只需要关心做什么,而不需要关心任何其他代码做什么。init没有理由这样做[self retain],也不必“担心”任何其他代码的作用。时期。

那么_completionHandler = handler; _identifiers = identifiers;就错了。如果将块存储在实例变量中,则需要复制该块;并且该集合需要保留或复制。您需要执行 _completionHandler = [handler copy]; _identifiers = [identifiers retain];使用 setter self.completionHandler = handler; self.identifiers = identifiers;

那么,就不存在“保留周期”的问题了。保留循环需要一个循环——A保留B,B保留A。块保留self,但self保留块?我在任何地方都没有看到。您只是在此块上调用另一个类的类方法。所以你不应该做弱参考。无论如何,弱引用都是不正确的,因为不能保证当前对象在块执行时是有效的。

似乎您(错误地)做了整个[self retain]事情,都是为了处理您(也错误地)不允许块保留的事实self,因为它应该。只要去掉这个弱引用的东西,去掉这些[self retain]东西,它不仅会遵循内存管理规则,更健壮,而且看起来更干净,更简单,更容易理解。

于 2012-10-12T08:54:24.747 回答
0
@property (nonatomic, copy) StuffInfoDownloaderCompletionBlock
completionHandler;

然后在初始化:

self.completionHandler = handler;

如果你以前没有复制过它,你永远不应该保留它,那没有意义。

顺便一提

 if ((self = [super init])) {
    /* initialization stuff*/
    }


 return self;

似乎你的代码有很多retainCycle缺陷设计

于 2012-10-11T11:20:24.573 回答