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

发布于 2024-08-10 11:23:45 字数 4908 浏览 11 评论 0原文

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

I am using a private MBProgressHUD

Now I am using the indicator view on my add button in which I am calling my addrecord service .

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

the adding to favorites method :

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

This is all running fine except the instruments gives leak of the uialertview may be it is conflicting with the mbprogreshud.

So I thought to remove the alert from the calling method and put it in the caller the method like this:

the caller method now :

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

the adding to favorites method :

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

In the launching of the progresshud thread is detaching something like this :

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

Now My question is that If I follow the first scenario . How can I assure the the alertview leak will not come

Or If I am following the second scenario How can I assure the if condition will be executed after completing this line executed :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-08-17 11:23:45

对于第一种情况,从应用程序主线程以外的线程进行 UI 更新通常是一个坏主意。 UIKit 不是线程安全的,执行线程 UI 更新可能会导致各种奇怪的事情发生。现在,我不确定这是否是泄漏的原因,但我会避免在addingToFavorites 中显示 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];

但我会选择第二种情况。

Regarding the first scenario, it is in general a bad idea to do UI updates from threads other than the applications main thread. UIKit is NOT thread safe and doing threaded UI updates can cause all sorts of strange things to happen. Now, I'm not sure if this is the cause for the leak but I would avoid showing an UIAlertView in addingToFavorites. Use performSelectorOnMainThread or the second scenario described below.

Regarding the second scenario, move everything below the showWhileExecuting call to the hudWasHidden delegate method. At this point you can be sure that your code was fully executed and the doneFlag was set.

To use performSelectorOnMainThread, define a new method, put your code in it and than call performSelectorOnMainThread.

I.e.,

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

Call with,

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

I would go with the second scenario though.

柠檬心 2024-08-17 11:23:45

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

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

应交换最后两行:

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

Other answers notwithstanding, you were creating the UIAlertView leak with this sequence:

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

The last two lines should be swapped:

[alert show];
[alert release];
alert = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文