Blocks in nsdictionary?

So, I store the blocking actions in an nsmutabledictionary, and then remember them when the response returns to the websocket. This turns an asynchronous request into block syntax. Here's the stripped down code:

- (void)sendMessage:(NSString*)message responseAction:(void (^)(id))responseAction
{ 
    NSString *correlationID =  (NSString*)[[message JSONValue] objectForKey:@"correlationId"];

    [self.messageBlocks setObject:responseAction forKey:correlationID];

    NSLog(@"Sending message: %@", correlationID);
   [webSocket send:message];
}

- (void)webSocket:(SRWebSocket *)wsocket didReceiveMessage:(id)message;
{
    NSString *correlationID = (NSString*)[[message JSONValue] objectForKey:@"correlationId"];
    NSLog(@"Incoming message. CorrelationID: %@", correlationID);
    void (^action)(id) = nil;
    if (correlationID) {
        action = [messageBlocks objectForKey:correlationID];
        if (action) action([message JSONValue]);
        [messageBlocks removeObjectForKey:correlationID];
    }
}

Note. The server responds with a correlator identifier that is sent with the request. Thus, each response is associated with each request through this identifier.

This works fine, better than I expected. My question is, is it safe to run blocks this way? Calls [messageBlocks removeObjectForKey: correIDID]; enough to remove it from memory. I remember pre-ARC, block_release is an option.

+5
source share
1 answer

, .

[self.messageBlocks setObject:[responseAction copy] forKey:correlationID];

, ARC, -autorelease.

[self.messageBlocks setObject:[[responseAction copy] autorelease] forKey:correlationID];

, .

+8

All Articles