私のアプリでは、NSDictionaries を共有およびマージする 2 つ (またはそれ以上) のデバイスを使用する予定です。Multipeer Connectivity は問題なく動作していますが、転送ごとに 2 つの辞書をマージするときに問題が発生しています。現在、両方の辞書を反復処理する 2 つの "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;
}
}
私が抱えている問題は、2 つの NSDictionaries の間に複数の同様のキー/値のペアがある場合に発生します。これにより、UIAlertView が立て続けに数回作成されますが、これは明らかに良いことではありません。UIAlertView の各作成を遅らせる方法はありますか? それよりも明白な解決策はありますか?コード ブロックを使用してみましたが、UIAlertView で "(null)" と表示されるため、コード ブロックは "key1" と "key2" の値を読み取るようには見えません。[alert1 show] の後の行でもこのアプローチを試しました。
while ((!alert1.hidden) && (alert1.superview != nil))
{
[[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];
}
しかし、UIAlertView が 2 回作成されるまでは機能しないようです。私が得ているエラーは次のとおりです。
-[UIKeyboardTaskQueue performTask:]、/SourceCache/UIKit/UIKit-2903.23/Keyboard/UIKeyboardTaskQueue.m:388 でのアサーションの失敗
キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: '-[UIKeyboardTaskQueue performTask:] は、メイン スレッドからのみ呼び出すことができます。'
どんな提案でも大歓迎です。すべて試してみます。私が抱えていたのと同じ問題を抱えている他の多くの人々を見つけることができませんでした。