1

アーカイブから読み取り、含まれているファイルを解凍し、それぞれに対応するコア データ エンティティを作成するインポート シーケンスがあります。このプロセス全体がバックグラウンドで発生し、スレッドごとに個別のコンテキストが作成されるなど、すべて正常に機能します。

この特定のインポート シーケンスの望ましい機能は、入力ファイルのいずれかをパスワードで保護できるようにすることであることが判明しました (アーカイブにはいくつかのファイルが含まれています)。そのため、ファイルがパスワードで保護されているかどうかを確認する必要があります。ユーザーは、 経由でパスワードを入力するよう求められますUIAlertView

これが私の問題の始まりです。

UIAlertView必要に応じてメイン スレッドにプロンプ​​トを送信し、インポーターobjectを として割り当てdelegate、ユーザー入力を待ちます。

ユーザーがパスワードを入力して [OK/キャンセル] をタップすると、デリゲート コールバックがまだメイン スレッド上にあるため、多くの作業 (管理対象オブジェクト ID への参照の保存など) を行わないと、対応するコア データ エンティティを操作できなくなります。文脈など)。

私の質問:

インポート プロセスが機能している元のバックグラウンド スレッドに戻ることはできますか? どうすればいいですか?

ありがとう、ログ

4

1 に答える 1

3

ディスパッチ セマフォを使用してみます。インスタンス変数に保存します。

@interface MyClass ()
{
    dispatch_semaphore_t dsema;
}
@end

次に、バックグラウンド スレッド メソッドで次のようにします。

// this is the background thread where you are processing the archive files
- (void)processArchives
{
    ...
    self.dsema = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"Title"
                                                                ...
                                                           delegate: self
                                                                ...
        ];
        [alertView show];
    });

    dispatch_semaphore_wait(self.dsema, DISPATCH_TIME_FOREVER);
    // --> when you get here, the user has responded to the UIAlertView <--

    dispatch_release(self.dsema);
    ...
}

は、UIAlertViewこのデリゲート メソッドを呼び出します。

// this is running on the main queue, as a method on the alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // do stuff with alertView
    if (buttonIndex == [alertView firstOtherButtonIndex]) {
        ...
        // when you get the reply that should unblock the background thread, unblock the other thread:
        dispatch_semaphore_signal(self.dsema);
        ...
    }
}
于 2012-10-21T19:14:26.057 に答える