0

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

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

4

1 回答 1

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];
});
于 2013-11-05T20:36:12.680 回答