假设我有以下 typedef:
typedef void (^myBlock)(id);
我有一些方法myMethod接受myBlock作为参数并产生一些id变量:
void myMethod(id myObj, myBlock block) {
// ...
block(myObj);
// ...
}
因此,如果我有以下内容:
- (void) someMethod {
__block id myObj; // Some initialization local to the scope of someMethod;
// myObj = ... some assignment to myObj so it is not nil...
dispatch_async(someQueue(), ^{
// (*) Some long, possibly multi-queued and multi-blocked, processing,
// so we are sure that someMethod will run out:
// ...
// 1. Can I be sure that myObj is still alive and actual here
// after the long processing (*)?
myMethod(myObj, ^(id myObj) {
// (**) Some long, possibly multi-queued, processing here too...
// 2. Can I be sure that myObj is still alive and actual here
// after the long processing (**) finishes?
})
})
}
我是否必须特别保留 myObj以便它可以跨越不同的队列/块?
抱歉,如果我问的是一些明显且有足够记录的东西 - 我刚刚开始学习 Objective-C,当时 ARC 可能是默认设置,所以它不需要我太关心这些保留计数、自动释放和其他东西,并且只在我在这里描述的情况下考虑它们。