Construct NSInvocation w/ Block argument
I'm trying to send a Block as an argument to a method called by an NSInvocation (which, for context, is fired by an NSInvocationOperation). The invocation should be retaining the arguments, and it seems to be working for the "regular" object parameters, but the Block's retainCount is staying at 1.
I could release it after it is used in the method call, but that could theoretically leak it if the queue is dissolved before the operation is called.
Some code:
NSInvocationOperation *load = [[NSInvocationOperat开发者_开发百科ion alloc] initWithInvocation:loadInvoc];
NSAssert([loadInvoc argumentsRetained],@"Arguments have not been retained");
[loader release];
NSInvocation *completionInvoc = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(serviceCompletionBlock:afterInvocationCompleted:)]];
[completionInvoc setTarget:self];
[completionInvoc setSelector:@selector(serviceCompletionBlock:afterInvocationCompleted:)];
MFEImageCallback callback = [completionBlock copy];
[completionInvoc setArgument:&callback atIndex:2];
[completionInvoc setArgument:&load atIndex:3];
NSInvocationOperation *completion = [[NSInvocationOperation alloc] initWithInvocation:completionInvoc];
NSAssert([completionInvoc argumentsRetained],@"Completion handler not retaining");
[callback release];
[completion addDependency:load];
The block that I'm using (defined in an accessor method for an NSManagedObject
subclass):
^(UIImage *image,NSError *err){
[self setValue:image forKey:key];
}
Do not call `retainCount`; it is useless.
Without seeing the contents of your block, it is impossible to say. If your block is effectively a static block, then copying it does nothing.
Are you seeing a crash?
精彩评论