ios - sendAsynchronousRequest method will excute block code before block outside code -
When I use the Asynchronous requests always run in the network thread. Your [NSOperationQueue mainQueue] means that the block-callback will be scheduled in the main key. If you want to wait until the request ends, you can use synchronous requests, or use the semaphore to block the current thread. But note that this will block the current thread for the request Be careful when using this kind. sendAsynchronousRequest method that will execute the method code out of the inner block Will execute?
__ Block NSDictionary * dict; [NSURLConnection sendAsynchronousRequest: Request line: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * reaction, NSData * data, NSError * connectionError) {NSInteger httpStatus = [((NSHTTPURLResponse *) response) statusCode]; NSLog (@ "inside httpStatus block:% d", httpStatus); If ([data length] & gt; 0 & amp; & amp; connectionError == null) {dict = [NSJSONSerialization JSONObjectWithData: data base: NSJSONReadingMutableContainers | NSJSONReadingAllowFragments Error: Zero]; } Else if (connectionError) {UIAlertView * alt = [[UIAlertView alloc] initWithTitle: @ "error" message: [connectionError localizedDescription] representatives: self cancelButtonTitle: @ "OK" OtherButtonTitles: zero, zero]; [Alt Show]; }}]; Return dict; // This will return zero, because it will run before the sendAsynchronousRequest internal block execution
+ (NSData *) sendSynchronousRequest :( NSURLRequest *) requested the return: (NSURLResponse **) response error: (NSError **) error;
Comments
Post a Comment