快速连续多次创建UIAlertView

时间:2013-11-05 18:47:18

标签: ios objective-c uialertview

在我的应用程序中,我打算让两个(或更多)设备共享和合并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:]只能从主线程调用。

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

1 个答案:

答案 0 :(得分:5)

您可以在创建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];
});