我有一个名为 RestClient 的类,它实现了 NSURLConnectionDataDelegate。
在 CCLayerColor 类 Menu(我的 cocos2d 应用程序的菜单)中,我有一个 RestClient 类型的属性,定义如下:
@property(nonatomic, strong) RestClient *rc;
在 onEnterTransitionDidFinish 我有以下代码:
[super onEnterTransitionDidFinish];
AppController *appDel = (AppController *)[[UIApplication sharedApplication] delegate];
if (appDel.FreeVersion) {
if (!self.rc) {
self.rc = [[RestClient alloc] init]; //released in dealloc
}
[rc GetMessage];
}
Menu 类的 dealloc 如下所示:
- (void) dealloc
{
NSLog(@"Menu dealloc");
[rc release];
[super dealloc];
}
退出菜单后,我看到调用了 dealloc,但是在 [rc release]之后,我没有看到 RestClient触发的 dealloc。知道为什么吗?
这是 RestClient 类中的代码:
@implementation RestClient
-(void)GetMessage
{
NSString *lng = NSLocalizedString(@"lng",nil);
NSString *urlStr = [NSString stringWithFormat:@"http://MyLink/%@", [lng uppercaseString]];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[NSURLConnection connectionWithRequest:req delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
messageData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[messageData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//show the message
if (!errorReceivingMessage) {
id json = [NSJSONSerialization JSONObjectWithData:messageData options:kNilOptions error:nil];
msgDict = (NSDictionary*)json;
NSString *msgText = [msgDict objectForKey:@"MessageText"];
NSString * msgTitle = [msgDict objectForKey:@"MessageTitle"];
NSString * buyButtonText = [msgDict objectForKey:@"BuyButtonText"];
NSString *cancelButtonText = [msgDict objectForKey:@"CancelButtonText"];
[messageData release];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:msgTitle message:msgText delegate:self
cancelButtonTitle:cancelButtonText
otherButtonTitles: buyButtonText , nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"https://someLink"]];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (messageData) {
[messageData release];
}
errorReceivingMessage = YES;
}
-(void)dealloc
{
NSLog(@"Dealloc RestClient");
//[messageData release];
[super dealloc];
}
@end