1

我正在使用私人MBProgressHUD

现在,我在调用 addrecord 服务的添加按钮上使用指示器视图。

UIWindow *window = [UIApplication sharedApplication].keyWindow;
HUD = [[MBProgressHUD alloc] initWithWindow:window];

// Add HUD to screen
[window addSubview:HUD];

// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;

// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(addingToFavorites) onTarget:self withObject:nil animated:YES];

添加到收藏夹方法:

NSURL *url = [NSURL URLWithString:urlstring];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
//[request setTimeoutInterval:10];
//NSURLResponse *response = nil;
//      NSError *error = nil;
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSData *data1= [NSURLConnection sendSynchronousRequest:request          
                                     returningResponse:nil error:nil];

if(data1 == nil)
{
    doneFlag = NO;
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert"
                                                   message:@"The network is not available.\n Please check the Internet connection."
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
    [alert show];
    [alert release];

}
else
{
    doneFlag = YES;

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Confirmation"
                                                   message:@"Added To favorites" 
                                                  delegate:nil
                                         cancelButtonTitle:@"OKAY"
                                         otherButtonTitles:nil];
    [alert show];
    alert = nil;
    [alert release];        
}

[request release];

这一切都运行良好,除了仪器泄漏 uialertview 可能与 mbprogreshud 冲突。

所以我想从调用方法中删除警报并将其放在调用者中,方法如下:

现在调用者方法:

UIWindow *window = [UIApplication sharedApplication].keyWindow;
HUD = [[MBProgressHUD alloc] initWithWindow:window];

// Add HUD to screen
[window addSubview:HUD];

// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;

// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
//it should wait for the above line to be executing   ******* then to exexute the be //below condition but how ?

if (doneFlag == NO) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert"
                                                   message:@"The network is not available.\n Please check the Internet connection."
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
    [alert show];
    [alert release];
} else {
    [favoritesButton setTitle:@"Remove" forState:UIControlStateNormal];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Confirmation"
                                                   message:@"Added To favorites" 
                                                  delegate:nil
                                         cancelButtonTitle:@"OKAY"
                                         otherButtonTitles:nil];
    [alert show];
    alert = nil;
    [alert release];        
}

添加到收藏夹方法:

NSURL *url = [NSURL URLWithString:urlstring];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
//[request setTimeoutInterval:10];
//NSURLResponse *response = nil;
//      NSError *error = nil;
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSData *data1= [NSURLConnection sendSynchronousRequest:request          
                                     returningResponse:nil error:nil];

if(data1 == nil)
{
    doneFlag = NO;
}
else
{
    doneFlag = YES;
}

[request release];

在progresshud线程的启动中分离了这样的东西:

[NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil]

现在我的问题是,如果我遵循第一种情况。我怎样才能保证alertview泄漏不会发生

或者,如果我遵循第二种情况,如何确保在完成此行执行后执行 if 条件:

[HUD showWhileExecuting:@selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
4

2 回答 2

1

对于第一种情况,从应用程序主线程以外的线程进行 UI 更新通常是个坏主意。UIKit 不是线程安全的,并且执行线程 UI 更新可能会导致各种奇怪的事情发生。现在,我不确定这是否是泄漏的原因,但我会避免在 addToFavorites 中显示 UIAlertView。使用 performSelectorOnMainThread 或下面描述的第二种情况。

关于第二种情况,将 showWhileExecuting 调用下方的所有内容移至 hudWasHidden 委托方法。此时,您可以确定您的代码已完全执行并且已设置 doneFlag。

要使用 performSelectorOnMainThread,请定义一个新方法,将代码放入其中,然后调用 performSelectorOnMainThread。

IE,

- (void)showAlert {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

打电话,

[self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];

不过,我会选择第二种情况。

于 2009-11-09T11:27:05.523 回答
1

尽管有其他答案,但您正在使用以下序列创建 UIAlertView 泄漏:

[alert show];
alert = nil;
[alert release];

最后两行应该交换:

[alert show];
[alert release];
alert = nil;
于 2009-11-09T13:03:56.847 回答