解决iPhone中分离线程的等待情况?

时间:2009-11-09 10:50:15

标签: iphone memory-management uiprogressview

我使用私人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];

2 个答案:

答案 0 :(得分:1)

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

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

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

即,

- (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];

我会选择第二种情况。

答案 1 :(得分:1)

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

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

最后两行应交换:

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