我做了一些示例代码,结果可能会让您感兴趣。
我创建了一个创建请求的类,就像你的一样:
@implementation Aftest
@synthesize name = _name;
- (void) doSomeStuff
{
NSLog(@"Got here %@", self.name);
}
- (void)startDownload
{
self.name = [NSString stringWithFormat:@"name"];
NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2"];
NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id JSON)
{
[self doSomeStuff];
} failure:^(NSHTTPURLResponse *response, NSError *error)
{
NSLog(@"%@", [error localizedDescription]);
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
}
@end
并称之为:
Aftest *af = [[Aftest alloc] init];
NSLog(@"1 - retain count %d", [af retainCount] );
[af startDownload];
NSLog(@"2 - retain count %d", [af retainCount] );
[af release];
NSLog(@"3 - retain count %d", [af retainCount] );
我得到的结果是:
2011-10-09 09:28:41.415 aftes[6154:f203] 1 - retain count 1
2011-10-09 09:28:41.418 aftes[6154:f203] 2 - retain count 2
2011-10-09 09:28:41.419 aftes[6154:f203] 3 - retain count 1
2011-10-09 09:28:43.361 aftes[6154:f203] Got here name
您的对象在块内传递时应保留。它应该避免这些崩溃。
cancel
无论哪种方式,正如迈克尔回答的那样,只要您有权访问操作对象,就应该可以调用。