EXC_BAD_ACCESS on block execution

I have a class with a single method that uses a URLConnection to send a serialized NSDictionary to a script at a specific URL, and then calls a termination block. Here is the code for this method:

- (void)sendDictionary:(NSDictionary *)dictionary toScript:(NSString *)scriptName completion:(void (^) (id response))completionBlock
{

    ...Serialize data and add it to an NSURLRequest request...

    H2URLConnection *connection = [[H2URLConnection alloc]initWithRequest:request];

    //Define a semaphore to block execution of later statements until the signal is received.
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);

    [connection setCompletionBlock:[^(id obj, NSError *err)
     {
         if (!err) {
            //Catch the server response
             NSString *receivedString = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
             NSLog( @"ChecklistAppNetworkManager received string: %@", receivedString);

             //Convert the JSON response into an NSDictionary
             NSError *otherError;
             id deserializedJSON = [NSJSONSerialization JSONObjectWithData:obj options:kNilOptions error:&otherError];

             if (otherError) {
                 NSLog(@"ChecklistAppNetworkManager JSON Error: %@", otherError.description);
             }

             [completionBlock invoke];

             NSLog(@"ChecklistAppNetworkManager JSON Response: %@", deserializedJSON);

             //Dispatch the semaphore signal so that the main thread continues.
             dispatch_semaphore_signal(sem);

         } else {
             NSLog(@"ChecklistAppNetworkManager encountered an error connecting to the server: %@", [err description]);
         }

     }copy]];
    //Finalize and initate the connection.
    [connection start];

    //Since block is dispatched to main queue, stall with a loop until the semaphore signal arrives.
    while (dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW)) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    }
}

I am trying to call this method on an instance of this class from another class where the completion block is defined. Here is the code where I get EXC_BAD_ACCESS:

- (void)doSomeServerTask
{
    H2ChecklistAppNetworkManager *currentNetworkManager = ((H2AppDelegate *)[[UIApplication sharedApplication]delegate]).networkManager; //Instantiate class where that method is defined

    NSMutableDictionary *dictonary = [NSMutableDictionary dictionary];

    ...populate dictionary...

    [currentNetworkManager sendDictionary:dictionary toScript:@"script.php" completion:[^(id response)
     { //THIS iS THE LINE WHERE THE BAD ACCESS OCCURS

         NSLog(@"LoginViewController received response: %@", response);

     } copy]];
}

Any help would be appreciated!

+4
source share
1 answer

completionBlock , invoke. , - , , , .


, . . sub-runloop MEL; .

. , , , .

, :

• "..." -

• " UI"

• " UI", "...." UI

, .

+5

Source: https://habr.com/ru/post/1620908/


All Articles