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]

今私の質問は、私が最初のシナリオに従う場合です。アラートビューのリークが発生しないことをどのように保証できますか

または、2番目のシナリオに従っている場合、この行の実行が完了した後にif条件が実行されることをどのように保証できますか?

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

2 に答える 2

1

他の回答にもかかわらず、次のシーケンスでUIAlertViewリークを作成していました。

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

最後の2行を入れ替える必要があります。

[alert show];
[alert release];
alert = nil;
于 2009-11-09T13:03:56.847 に答える
1

最初のシナリオに関しては、一般に、アプリケーションのメインスレッド以外のスレッドからUIの更新を行うことはお勧めできません。UIKitはスレッドセーフではなく、スレッド化されたUI更新を行うと、あらゆる種類の奇妙なことが起こる可能性があります。これがリークの原因かどうかはわかりませんが、addingToFavoritesでUIAlertViewを表示することは避けます。PerformSelectorOnMainThreadまたは以下で説明する2番目のシナリオを使用します。

2番目のシナリオに関しては、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];

ただし、2番目のシナリオを使用します。

于 2009-11-09T11:27:05.523 に答える