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];
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[connection setCompletionBlock:[^(id obj, NSError *err)
{
if (!err) {
NSString *receivedString = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
NSLog( @"ChecklistAppNetworkManager received string: %@", receivedString);
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_semaphore_signal(sem);
} else {
NSLog(@"ChecklistAppNetworkManager encountered an error connecting to the server: %@", [err description]);
}
}copy]];
[connection start];
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;
NSMutableDictionary *dictonary = [NSMutableDictionary dictionary];
...populate dictionary...
[currentNetworkManager sendDictionary:dictionary toScript:@"script.php" completion:[^(id response)
{
NSLog(@"LoginViewController received response: %@", response);
} copy]];
}
Any help would be appreciated!
source
share