我在我的操作队列(_opQueue)中创建了一些东西,我也想在这个队列中释放它们。这是我的代码,_opQueue 是从 "dispatch_queue_create("Data_Serial_Operation_Queue", DISPATCH_QUEUE_SERIAL);" 创建的
@implementation CrowdPot
- (void) dealloc {
[_queryString release],_queryString = nil;
[_sortedItems release],_sortedItems = nil;
objc_setAssociatedObject(_indexesDictionary, ctIndexDictionaryTypeKey, nil, OBJC_ASSOCIATION_ASSIGN);
[_indexesDictionary release],_indexesDictionary = nil;
[super dealloc];
}
- (id)init
{
self = [super init];
if (self)
{
dispatch_sync(_opQueue, ^{
_sortedItems = [[NSMutableArray alloc] initWithCapacity:128];
_indexesDictionary = [[NSMutableDictionary alloc] initWithCapacity:28];
_initialRange = NSMakeRange(NSNotFound, 0);
_needRefill = YES;
});
}
return self;
}
- (oneway void)release
{
dispatch_async(_opQueue, ^{
[super release];//revise
});
}
在发布方法中,我在一个块中使用“[super release]”,我认为它可能会保留 self(使用关键字“super”保留 self 的保留计数)。我认为这不是一个优雅的实现,甚至是错误的。
那么如果我想在我的_opQueue中释放这些数据,如何编写释放方法呢?在这种情况下有另一种方法吗,有人帮忙吗?
这是一种在我从互联网上获得的特定线程中发布数据的方法。
-(oneway void)release{
//ensure dealloc occurs on our _addressBook thread
//we do this to guarantee that we are removed from the weak cache before someone else ends up with us.
if (_addressBookThread && ![[NSThread currentThread] isEqual:_addressBookThread]) {
[self performSelector:_cmd onThread:_addressBookThread withObject:nil waitUntilDone:NO];
} else {
[super release];
}
}