これは、ブロックに関連するメモリ管理に関する些細な質問の 1 つであり、いつどこfc
で解放する必要があるのか わかりません
NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSError *error = nil;
[fc coordinateWritingItemAtURL:sourceURL
options:NSFileCoordinatorWritingForDeleting
error:&error
byAccessor:^(NSURL *newURL) {
// if error is not nil this block will not be called
NSError *anError = nil;
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtURL:newURL error:&anError];
dispatch_async(q_main, ^{
// change to the main queue and update the UI
completion(anError);
});
// *** (1) Release here ? ***
// [fc release];
}];
// *** (2) or Release here ? ***
// [fc release]
if (error) {
// change to the main queue and update the UI
dispatch_async(q_main, ^{
completion(error);
});
}
(1)でリリースすればOK(リークなし)だと思いますが、これは本当に標準的なやり方なのでしょうか? (同じオブジェクトが呼び出すブロック内のオブジェクトを解放する??)。ここに何か違和感を感じます。
(2)でもOKですが、アクセサーブロックが同期的に呼び出されるためです。
学習目的で...アクセサーブロックが非同期で呼び出されるとどうなりますか? (NSFileCoordinatorの必要のない架空のケース)そのような場合、ivarを作成fc
する必要がありますか、それとも最初のアプローチとしてそれを実行しても問題ありませんか?
どんな助けでも大歓迎です
:)