// Capture a strong reference to someView, to make sure it's still around later on.
__block UIView *v = someView;
//Need to go back to the main thread since this is UI related
dispatch_async(dispatch_get_main_queue(), ^{
[v addSubview:self]
// Release v (assuming ARC). If nothing else has retained someView, this means
// that someView will be deallocated - but we're on the main thread,
// so that's fine. What we don't want is a background dealloc.
v = nil;
});
OR
// Capture a strong reference to someView, to make sure it's still around later on.
__weak UIView *weakRef = someView;
//Need to go back to the main thread since this is UI related
dispatch_async(dispatch_get_main_queue(), ^{
// This is thread-safe because someView should only be de-alloced on the main thread.
// consequently our access to weakRef is safe.
UIView *strongRef = weakRef;
[strongRef addSubview:self];
});
什麼不可以做的就是這個。
UIView *strongRef = whatever;
dispatch_async(downloadQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[someView addSubview:self]
});
// If, by the time we get here, the dispatch to the main queue has already run,
// then we will send a release to someView on a background thread. If it's the last
// remaining reference - and it might be, unlikely as it is, then we just dealloced
// a UIView in the background. This is bad.
});
如果你使用ARC並且'someView'被聲明爲'weak'引用,那麼如果對象被釋放,它將是'nil'。這將避免保持對不再存在的視圖的引用的問題。 – rmaddy 2013-04-30 16:19:38
這是正確的。我將該屬性定義爲「分配」而不是弱。改變爲弱解決了這個問題。 – soleil 2013-04-30 16:33:58
在ARC中使用「assign」有效地消除了ARC的所有優勢! :) – matt 2013-04-30 17:08:11