-1

I created some something in my operation queue(_opQueue), and i want to release them in this queue too. Here is my code, _opQueue was created form "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
    });
}

In the release method I use "[super release]" in a block, which I think might retain self(using the keyword 'super' retain self's retain count). I think it's not a elegance implementation and even wrong.

Then how to write the release method if i want to release these data in my _opQueue? Is another way to do this in this case, anyone help?

Here is a way to release data in a specific thread which i got it from internet.

-(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];
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
bigjava
  • 145
  • 2
  • 10
  • 1
    Huh? <-- This is all I can say. An object's retain count has nothing to do with threads. Just release them as you normally would. – borrrden Jun 10 '13 at 07:42

2 Answers2

0

There might be a better solution but I had a more or less similar case that I only used for debugging though:

When I create an object I also put it into some container (NSMutableSet) which retains the object. Then I override the release method and check the object's retain count. If retain count is one (only container array is retaining it) I perform selector on certain thread to remove the object from that container array and forcing it to be deallocated on that same thread.

Be careful though not to modify the array from a different thread.

I hope this helps.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
0

I think I've got your idea.....
Try to release it with this:

- (oneway void)releaseOnMyQueue {
    __block id temp = self;
    dispatch_async(_opQueue, ^{
        [temp release];
    });
}
LeverkusenFan
  • 293
  • 4
  • 8