快速连续多次创建 UIAlertView

Posted

技术标签:

【中文标题】快速连续多次创建 UIAlertView【英文标题】:Creating UIAlertView multiple times in a quick succession 【发布时间】:2013-11-05 18:47:17 【问题描述】:

在我的应用程序中,我打算让两个(或更多)设备共享和合并 NSDictionaries。我的 Multipeer Connectivity 工作完美,当归结为在每次传输时合并两个字典时,我遇到了一个问题。我现在有两个“for”循环遍历两个字典。如果存在已经存在的键/值对,则会提示用户是否要覆盖已存在的对象或保留它。他们可以选择保留当前对象、覆盖当前对象、保留所有冲突对象或覆盖所有冲突对象。我到目前为止的代码:

-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID

pathChooser = 1;

NSLog(@"DATA RECEIVED: %d bytes!", data.length);

dataReceived = data;

receivedDataDict = [[NSMutableDictionary alloc] init];

receivedDataDict = [NSKeyedUnarchiver unarchiveObjectWithData:dataReceived];

for (key1 in receivedDataDict) 

    NSLog(@"%@", key1);

    if ([dataDict objectForKey:key1] == nil) 

        NSLog(@"Writing new folder");

        [dataDict setObject:[[NSMutableDictionary alloc] init] forKey:key1];

    

    for (key2 in [receivedDataDict objectForKey:key1]) 

        if ([[dataDict objectForKey:key1] objectForKey:key2] == nil) 

            NSLog(@"Writing a new file");

            [[dataDict objectForKey:key1] setObject:[[receivedDataDict objectForKey:key1] objectForKey:key2] forKey:key2];

        

        else

            if (pathChooser == 1) 

                NSLog(@"MADE IT TO -ALREADY EXISTS-");

                UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle: [[NSString alloc] initWithFormat:@"Match %@ already exists in %@!", key2, key1]

                                                           message: @"Overwrite?"

                                                          delegate: self

                                                 cancelButtonTitle:@"Keep"

                                                 otherButtonTitles:@"Overwrite", @"Keep All", @"Overwrite All",nil];

                [alert1 show];

            

            else if (pathChooser == 3)

                NSLog(@"Path 3");

                [[dataDict objectForKey:key1] setObject:[[receivedDataDict objectForKey:key1] objectForKey:key2] forKey:key2];

            

        

    



[dataDict writeToFile:path atomically:YES];

NSLog(@"%@", dataDict);

receivedDataDict = nil;





// Buttons for UIAlertView...

-(void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex

//Keep Current

if (buttonIndex == 0) 

    pathChooser = 1;





//Overwrite Current

else if (buttonIndex == 1) 

    [[dataDict objectForKey:key1] setObject:[[receivedDataDict objectForKey:key1] objectForKey:key2] forKey:key2];





//Keep All

else if (buttonIndex == 2) 

    pathChooser = 2;





//Overwrite All

else if (buttonIndex == 3) 

    pathChooser = 3;






当两个 NSDictionaries 之间存在多个相似的键/值对时,我遇到的问题会快速连续创建 UIAlertView 多次,这显然不是一件好事。有没有办法可以延迟 UIAlertView 的每次创建?还有比这更明显的解决方案吗?我尝试使用代码块,但代码块似乎没有读取“key1”和“key2”值,因为它们在 UIAlertView 中显示为“(null)”。我也在 [alert1 show] 之后的行中尝试过这种方法:

 while ((!alert1.hidden) && (alert1.superview != nil))

                    

                        [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

                    

但它似乎直到第二次创建 UIAlertView 之后才起作用。我得到的错误是:

-[UIKeyboardTaskQueue performTask:] 中的断言失败,/SourceCache/UIKit/UIKit-2903.23/Keyboard/UIKeyboardTaskQueue.m:388

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIKeyboardTaskQueue performTask:] 只能从主线程调用。”

欢迎任何建议,我会尽力而为。我找不到许多与我有同样问题的人。

【问题讨论】:

我在寻找解决方案时发现了这个:***.com/questions/19005463/… 【参考方案1】:

您总是可以在创建 UIAlertView 时执行“dispatch_async”。

所以,它最终看起来像:

__block UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:[[NSString alloc] initWithFormat:@"Match %@ already exists in %@!", key2, key1]
                                                        message:@"Overwrite?"
                                                       delegate:weakSelf
                                              cancelButtonTitle:@"Keep"
                                              otherButtonTitles:@"Overwrite", @"Keep All", @"Overwrite All",nil];
dispatch_async(dispatch_get_main_queue(), ^(void)

    [alert1 show];
);

【讨论】:

好的,到目前为止有效。没有错误了,非常感谢!但是,我将如何确保代码在用户响应之前不会超过 UIAlertView 块? (以便“覆盖所有”和“保留所有”选项生效并停止下一个警报视图)

以上是关于快速连续多次创建 UIAlertView的主要内容,如果未能解决你的问题,请参考以下文章

防止快速连续点击button多次执行相同操作

PHP和MySQL,快速连续多次检查行的有效方法?

iOS UIButton 禁止连续点击问题/快速点击/重复点击/多次点击 2021-12-03

SpringSecurity3无法拦截连续多次点击出起的请求

Android 如何判断多次点击

达到 eta 时,长 eta(8 小时以上)的 celery 任务会连续执行多次