Block switching thread in iOS - ios

I have troubles with multiple threads. Here is the situation:
I make asynchronous requests to backend and, in some cases, need to cancel these. Canceling the requests happens in a separate thread. All requests to the backend are canceled and, as I exit a screen, multiple class instances are deallocated.
When I have the order request-cancel, everything works fine. However, sometimes the cancel method is invoked when I am already in the middle of the finish method (which needs a little bit time because of decoding and conversion of data). In such cases, the app crashes, a messages to deallocated instance is sent. It is not an exception that can be easily cached, I get the crash even when I check for the existence of the instances a line before. Actually, if I understand it right, the instance of the class where the finish and the cancel method are located, is deallocated. Nevermind, the problem is that the thread is switched in the middle of the finish method and I need to prevent that.
My question is: Is there a way to block switching the thread in the middle of a method? To declare this method as a whole (transaction)? Or is there another standard solution for such a problem?
I read this post but I don't really understand whether it can be used in my case or how to do that.
EDIT: Canceling
for (XXX *request in [XXX sharedQueue].operations)
{
[request setDelegate:nil];
[request setDownloadProgressDelegate:nil];
[request setUploadProgressDelegate:nil];
[request setQueue:nil];
[request cancel];
}
XXX is the class used for the requests.
Order of the methods
Here is the order in which the methods are invoked (in the cases of error). Handler is the class for handling the requests.
Handler 1: make request
Handler 1: finish begin
Handler 2: cancel begin
Handler 2: cancel end
Handler 2: dealloc
Handler 1: dealloc
Handler 1: finish middle
Handler 1 and 2 are two instances of the class. The first one is deallocated in the middle of the finish method, so at the end of this I get a crash. Deallocating it is normal because after cancel I go to another view and basically everything gets deallocated.
My ideas for solution are either to somehow prevent going back to the finish method or to execute the entire finish method before switching the thread. Unfortunately, I have no idea how one of these could be implemented.

The following approach might help you:
add a flag to your controller managing all requests;
when a request finish block is entered do like this:
completionBlock:^() {
#synchronized(self.myFinishFlag) {
...
}
}
in your cancel method do like this:
-(void)userCancelledRequests:(id)sender {
#synchronized(self.myFinishFlag) {
...
}
}
This will delay the execution of 'userCancelledRequestsbody if a finish block is currently running (i.e., lockingself.myFinishFlag`).
Hope this helps.

It appears that the cancel method of class XXX is not correctly implemented.
Suppose there is some asynchronous operation of type XXX which responds to a cancel message. In order to function reliable, the following requirements must be fulfilled:
The cancel message can be send from a client from any thread.
The cancel message can be send at any time and multiple times.
When the operation receives the cancel message, it stops it asynchronous task and cleans up itself properly at the next "cancelation point". Note: this may happen asynchronously with respect to the cancel method. The implementation needs to be "thread safe"!
The receiver shall notify the delegate that it has been cancelled (for example in a failure handler).
Furthermore, there should be no need for a caller to reset the delegate(s) or prepare the receiver in any way before sending the cancel message.
These requirements needs to be fulfilled by the implementation of class XXX.
Now, suppose you have an internal finish method for that operation and lets suppose that this is the last method which will be executed by the operation. When that method is executed, and when the operation receives a cancel message concurrently, the implementation must guarantee that the cancel has no effect, since it is too late: the last opportunity to cancel the operation has been passed. There are a number of ways how to accomplish this.
If this is true, then the following code should properly cancel your operations:
for (XXX *request in [XXX sharedQueue].operations) {
[request cancel];
}
Edit:
Example for an "NSOperation like" implementation of a cancel and finish method:
Note:
Accessing ivars must be synchronized! Ivars will be accessed in various internal methods. All accesses will be serialized via a private serial dispatch queue, named "sync_queue".
- (void) cancel {
dispatch_async(self.sync_queue, ^{
if (_isCancelled || _isFinished) {
return;
}
[self.task cancel]; // will sets _isCancelled to YES
[self finish]; // will set _isFinished to YES
});
}
- (void) finish
{
assert(<execution context equals sync_queue>);
if (_isFinished)
return;
completionHandler_t onCompletion = self.completionHandler;
if (onCompletion) {
id result = self.result;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
onCompletion(result);
});
};
self.completionHandler = nil;
self.task = nil;
self.isExecuting = NO;
self.isFinished = YES;
}

The best way to block is to perform all operations on a dedicated queue. When you want to cancel, do it like this:
dispatch_async(_someQueue, ^{
for (XXX *request in [XXX sharedQueue].operations)
{
[request setDelegate:nil];
[request setDownloadProgressDelegate:nil];
[request setUploadProgressDelegate:nil];
[request setQueue:nil];
[request cancel];
}
});
Then when the finished callback is triggered, you can do this:
dispatch_async(_someQueue, ^{
if ([request isCancelled] == NO)
{
// Process request
}
});
So long as _someQueue is not marked as a concurrent queue, this should do the trick.

Related

NSInvocationOperation Cancel usage

Is it right if I cancel an NSInvocationOperation inside the operation?
example:
.h file:
//defined in the interface
NSInvocationOperation *op1;
NSOperationQueue *loadQueue;
.m file:
-(id)init{
op1 = [NSInvocationOperation new];
loadQueue = [NSOperationQueue new];
}
-(void)downloadData{
op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(loadServerResponse:) object:newerRequest];
[loadQueue addOperation:op1];
}
Now I have a method to download data from the server. I added a condition to check if there is an error. If so I'm cancelling that operation inside the method and calling back the same method upon retrieving the new login token.
- (void)loadServerResponse{
if(server_error){
[op1 cancel];
//fetch login token again
[self downloadData];
return;
}
Am I doing anything wrong?
First, referencing op1 (which I'm assuming is a class-level ivar) is a Bad Thing™ on multiple levels, the first of which is NSOperationQueue dispatches your operation onto a background thread, so op1 will (at best) be copied into that thread's context and no longer reference the original operation you were attempting to cancel.
I think you don't need to worry about canceling the operation, given the logic you have shared. You specifically seem to want to call downloadData after canceling, and there's no guarantee that will happen if you cancel the operation first. I would just remove the call to cancel the operation, and continue on. Normally you only cancel a running operation from an external source (say if you receive a notification that your application will enter background state).

NSOperationQueue : cancel an operation after a timeout given

Basically, I would like to perform a cancel if the operation I'm adding to the queue does not respond after a certain timeout :
NSOperationQueue * queue = ...
[self.queue addOperationWithBlock:^{
// my block...
} timeoutInSeconds:5.0 hasTimedOutWithBlock:^{
// called after 5.0, operation should be canceled at the end
}];
Thanks Guys !
You could do something like you asked for, but I might suggest adding a parameter to the first block by which the first block could check to see if the operation was canceled.
[queue addOperationWithBlock:^(NSOperation *operation) {
// do something slow and synchronous here,
// if this consists of a loop, check (and act upon) `[operation isCancelled]` periodically
} timeout:5.0 timeoutBlock:^{
// what else to do when the timeout occurred
}];
Maybe you don't need to check isCancelled, but in some cases you would (generally the burden for responding to a cancellation rests on the operation itself), so that's probably a prudent parameter to add.
Anyway, if that's what you wanted, you might do something like the following:
#implementation NSOperationQueue (Timeout)
- (NSOperation *)addOperationWithBlock:(void (^)(NSOperation *operation))block timeout:(CGFloat)timeout timeoutBlock:(void (^)(void))timeoutBlock
{
NSBlockOperation *blockOperation = [[NSBlockOperation alloc] init]; // create operation
NSBlockOperation __weak *weakOperation = blockOperation; // prevent strong reference cycle
// add call to caller's provided block, passing it a reference to this `operation`
// so the caller can check to see if the operation was canceled (i.e. if it timed out)
[blockOperation addExecutionBlock:^{
block(weakOperation);
}];
// add the operation to this queue
[self addOperation:blockOperation];
// if unfinished after `timeout`, cancel it and call `timeoutBlock`
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// if still in existence, and unfinished, then cancel it and call `timeoutBlock`
if (weakOperation && ![weakOperation isFinished]) {
[weakOperation cancel];
if (timeoutBlock) {
timeoutBlock();
}
}
});
return blockOperation;
}
#end
Having provided that code sample, I must confess that there are a very narrow set of situations where something like the above might be useful. Generally it would be better addressed using another pattern. The vast majority of the time, when you want a cancelable operation, you would implement a NSOperation subclass (often a concurrent NSOperation subclass). See the Defining a Custom Operation Object section of the Operation Queues chapter of the Concurrency Programming Guide for more information.

NSURLConnection does not call complete across multiple Views

Earlier today I asked the following question: iOS block being stoped when view pushed
The operation I mentioned (OP1) is actually a "http get" to my server, using NSURLConnection.
After even more investigation I discovered that the block doesn't actually "die". What really happens is that the request is actually SENT (the server side logs it), even after the view is pushed (verified via [NSThread sleep:10]). The server responds but then NOTHING happens on the app side if the view2 has been pushed! almost as if the connection had lost its delegate! Another possibility im looking at is "the fact that NSURLConnection is on the rsMainLoop related?"
Can anyone help?
Pls don't forget that:
0. Everything works fine as long as the view2 is not pushed until operation completion.
1. The request is sent async
2. I set the delegate and it works as long as the view dont change
3. view1 starts the operation using the "singleton object reference" property "OP1Completed"
4. view2 checks the completion of OP1 via propertie on the "singleton object reference"
5. view2 gets the "result" by going to the "singleton.OP1Result" property
Edit 1:
Ok lets have some code. First here is the relevant code of my singleton (named "Interaction"):
-(void)loadAllContextsForUser:(NSString *)username{
userNameAux = username;
_loadingContextsCompleted = NO;
if (contextsLoaderQueue == NULL) {
contextsLoaderQueue = dispatch_queue_create("contextsLoaderQueue", NULL);
}
dispatch_async(contextsLoaderQueue, ^{
NSLog(#"Loading all contexts block started");
[self requestConnectivity];
dispatch_async(dispatch_get_main_queue(), ^{
[Util Get:[NSString stringWithFormat:#"%#/userContext?username=%#", Util.azureBaseUrl, [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
successBlock:^(NSData *data, id jsonData){
NSLog(#"Loading all contexts block succeeded");
if([userNameAux isEqualToString:username]){
_allContextsForCurrentUser = [[NSSet alloc]initWithArray: jsonData];
}
} errorBlock:^(NSError *error){
NSLog(#"%#",error);
} completeBlock:^{
NSLog(#"load all contexts for user async block completed.");
_loadingContextsCompleted = YES;
[self releaseConnectivity];
}];
});
while (!_loadingContextsCompleted) {
NSLog(#"loading all contexts block waiting.");
[NSThread sleepForTimeInterval:.5];
}
});
NSLog(#"Load All Contexts Dispatched. It should start at any moment if it not already.");
}
And here is the class Util, which actually handles the request/response
-(id)initGet:(NSString *)resourceURL successBlock:(successBlock_t)successBlock errorBlock:(errorBlock_t)errorBlock completeBlock:(completeBlock_t)completeBlock;{
if(self=[super init]){
_data=[[NSMutableData alloc]init];
}
_successBlock = [successBlock copy];
_completeBlock = [completeBlock copy];
_errorBlock = [errorBlock copy];
NSURL *url = [NSURL URLWithString:resourceURL];
NSMutableURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
//[_conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
//[_conn start];
NSLog(#"Request Started.");
return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
id key = [[jsonObjects allKeys] objectAtIndex:0];
id jsonResult = [jsonObjects objectForKey:key];
_successBlock(_data, jsonResult);
_completeBlock();
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
_errorBlock(error);
_completeBlock();
}
And finally here is the relevant part VC1 (pushing in VC2)
- (IBAction)loginClicked {
NSLog(#"login clicked. Preparing to exibit next view");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
AuthenticationViewController *viewController = (AuthenticationViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ContextSelectionView"];
NSLog(#"Preparation completed. pushing view now");
[self presentViewController:viewController animated:YES completion:nil];
}
You might be surprised, but there are a couple of solutions - some of which are very common and can be implemented very easily ;) Even though, this answer is ridiculous elaborate, the actual solution to your problem will not exceed a few lines of code. :)
You ran into a typical "async problem" - well, it's less than a problem, rather a typical programming task nowadays.
What you have is an asynchronous task, OP1. This will be started from within ViewController 1 (VC1), and at some indeterminate time later, it will eventually produce either a result or an error.
The eventual result of OP1 should be handled later in VC2.
There are a few approaches how a client can obtain the eventual result, for example: via KVO, delegate method, completion block, callback function, future or promise and per notification.
These approaches above have one property in common: the call-site gets notified by the asynchronous result provider (and not vice versa).
Polling for the result until it is available, is a bad approach. Likewse, hanging in a semaphore and blocking the current thread until the result is "signaled" is equally suboptimal.
You are probably familiar with completion blocks. A typical asynchronous method which notifies the call-site when the result is available looks like this:
typedef void (^completion_block_t)(id result);
- (void) doSomethingAsyncWithCompletion:(completion_block_t)completionHandler;
Note: the call-site provides the completion handler, while the async tasks calls the block when it is finished, and passes its result (or error) to the result parameter of the block. Unless otherwise stated, the execution context - that is the thread or dispatch queue or NSOperationQueue - of where the block will be executed is not known.
But when thinking about your problem, a simple async function and a completion handler doesn't yield a viable solution. You cannot pass that "method" easily from VC1 to VC2 and then later "attach" somehow a completion block in VC2.
Luckily, any asynchronous task can be encapsulated into an NSOperation. An NSOperation has a completion block as a property which can be set by the call-site or elsewhere. And an NSOperation object can be easily passed from VC1 to VC2. VC2 simply adds a completion block to the operation, and eventually gets notified when its finished and the result is available.
However, while this would be a viable solution for your problem - there are in fact a few issues with this approach - which I don't want to elaborate, but instead propose an even better one: "Promises".
A "Promise" represents the eventual result of an asynchronous task. That is, a promise will exist even though the result of the asynchronous task is not yet evaluated. A Promise is an ordinary object which you can send messages. Thus, Promises can be passed around much like NSOperations. A Promise is the return value of an asynchronous method/function:
-(Promise*) doSomethingAsync;
Don't mismatch a Promise with the asynchronous function/method/task/operation - the promise is just a representation of the eventual result of the task.
A Promise MUST be eventually resolved by the asynchronous task - that is, the task MUST send the promise a "fulfill" message along with the result value, or it MUST send the promise the "reject" message along with an error. The promise keeps a reference of that result value passed from the task.
A Promise can be resolved only once!
In order to obtain the eventual result a client can "register" a success handler and an error handler . The success handler will be called when the task fulfills the promise (that is, it was successful), and the error handler will be called when the task rejected the promise passing along the reason as an error object.
Assuming a particular implementation of a promise, resolving a promise may look like this:
- (Promise*) task {
Promise* promise = [Promise new];
dispatch_async(private_queue, ^{
...
if (success) {
[promise fulfillWithValue:result];
}
else {
NSError* error = ...;
[promise rejectWithReason:error];
}
});
return promise;
}
A client "registers" handlers for obtaining the eventual result as follows:
Promise* promise = [self fetchUsers];
promise.then( <success handler block>, <error handler block> );
The success handler and error handler block are declared as follows:
typedef id (^success_handler_block)(id result);
typedef id (^error_handler_block)(NSError* error);
In order to just "register" a success handler (for the case, the async tasks "returns" successfully) one would write:
promise.then(^id(id users) {
NSLog(#"Users:", users);
return nil;
}, nil);
If the task succeeds, the handler will be called - which prints the users to the console.
When the task fails, the success handler will not be called.
In order to just "register" an error handler (for the case, the async tasks fails) one would write:
promise.then(nil, ^id(NSError* error) {
NSLog(#"ERROR:", error);
return nil;
}, nil);
If the task succeeds, the error handler will not be called. Only if the task fails (or any children tasks), this error handler will be invoked.
When the result of the async task is eventually available, the code within the handlers will be executed "in some unspecified execution context". That means, it may execute on any thread. (Note: there are ways to specify the execution context, say the main thread).
A promise can register more than one handler pair. You can add as many handlers as you want, and where and when you want. Now, you should understand the connection with your actual problem:
You can start an asynchronous task in VC1, and get a promise. Then pass this promise to VC2. In VC2 you can add your handler, which will get invoked when the result is eventually available.
Don't worry when the result is actually already available when passing the promise to VC2, that is, when the promise has been resolved already. You can still add handlers and they get fired properly (immediately).
You can also "chain" multiple tasks - that is, invoke task2 once when task1 is finished. A "chain" or "continuation" of four async tasks looks as follows:
Promise* task4Promise =
[self task1]
.then(^id(id result1){
return [task2WithInput:result1];
}, nil)
.then(^id(id result2){
return [task3WithInput:result2];
}, nil)
.then(^id(id result3){
return [task4WithInput:result3];
}, nil);
task4Promise represents the eventual result of task4WithInput:.
One can also execute tasks in parallel, like taskB and taskC which will get started in parallel when taskA has been finished successfully:
Promise* root = [self taskA];
root.then(^id(id result){
return [self taskB];
}, nil);
root.then(^id(id result){
return [self taskC];
}, nil);
With this scheme, one can define an acyclic graph of tasks, where each is dependent on the successful execution of its successor ("parent"). "Errors" will be passed through to the root, and handled by the last error handler (if any).
There are a few implementations for Objective-C. I've written one myself: "RXPromise" (available on GitHub). One of the strongest feature is "Cancellation" - which is NOT a standard feature of promises, but implemented in RXPromise. With this, you can selectively cancel a tree of asynchronous tasks.
There is a lot more about promises. You may search the web, especially in the JavaScript community.
I'm not sure I understand the work flow that goes on in the first controller -- specifically, what the user does to initiate the download, and what else he does before the next controller gets presented (and when that controller gets instantiated). When I've made apps in the past that required doing downloads from multiple classes, I've created a download class that creates the NSURLConnection, and implements all the call backs. It has one delegate protocol method to send back the data (either raw data or error object) to its delegate.
I made a simple test case simulating what I think your work flow is, using two buttons. One instantiates a Downloader class instance, creates the next controller, sets it as the delegate of the downloader, and starts the download. The second button does the push to that second controller. This works, no matter when the push happens, but I don't know if it's relevant to your situation (I test using the Network Link Conditioner to simulate a slow connection).
The first Controller:
#import "ViewController.h"
#import "ReceivingViewController.h"
#import "Downloader.h"
#interface ViewController ()
#property (strong,nonatomic) ReceivingViewController *receiver;
#end
#implementation ViewController
-(IBAction)buttonClicked:(id)sender {
Downloader *loader = [Downloader new];
self.receiver = [self.storyboard instantiateViewControllerWithIdentifier:#"Receiver"];
loader.delegate = self.receiver;
[loader startLoad];
}
-(IBAction)goToReceiver:(id)sender {
[self.navigationController pushViewController:self.receiver animated:YES];
}
The Download class .h:
#protocol DownloadCompleted <NSObject>
-(void)downloadedFinished:(id) dataOrError;
#end
#interface Downloader : NSObject
#property (strong,nonatomic) NSMutableData *receivedData;
#property (weak,nonatomic) id <DownloadCompleted> delegate;
-(void)startLoad;
Downloader .m:
-(void)startLoad {
NSLog(#"start");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) self.receivedData = [NSMutableData new];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.receivedData.length = 0;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.delegate downloadedFinished:error];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.delegate downloadedFinished:self.receivedData];
}
-(void)dealloc {
NSLog(#"In Downloader dealloc. loader is: %#",self);
}
The second controller:
#interface ReceivingViewController ()
#property (strong,nonatomic) NSData *theData;
#end
#implementation ReceivingViewController
-(void)downloadedFinished:(id)dataOrError {
self.theData = (NSData *)dataOrError;
NSLog(#"%#",self.theData);
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"%#",self.theData);
}
So, here is what I think will work for sure:
Pass the flag to the new controller. If the flag is unfinished, then start over loading in the new VC and make sure none of the data shows up until it is done loading.
I do think it is weird that the thread stops though, with the new VC being pushed, because when I dispatch asynchronous calls with AFNetworking, it does continue to load even after a new VC is pushed. Perhaps if you are using a different framework, you should use AFNetworking.
So, if your thread actually does continue after the new VC is pushed on (as I suspect it does - you just think it doesn't keep going because it crashes the code), then try the following:
a) pass flag, if operation finished, proceed normally
b) if not, don't load anything and invoke some kind of delegate method between the two that checks if the flag is set, and returns the data if so.
If you have questions on how to set up a delegate, just ask and I can fill in some details on that.
As already mentioned in a comment in you first question: you have probably two issues:
A design problem
A code issue, causing the block. (but without code this is difficult to figure out).
Lets propose a practical approach:
Say, our singleton is some "Loader" class which performs HTTP requests. Instead of polling a property which determines the state of the network request, you should return some object which you can ask for the state, or even better where VC2 can register a completion block which gets called when the request is finished.
An NSOperation could be "used" to represent the eventual result of the asynchronous network request. But this is a bit unwieldy - suppose we have a subclass RequestOperation:
RequestOperation* requestOp = [[Loader sharedLoader] fetchWithURL:url];
Now, "requestOp" represents your network request, including the eventual result.
You can obtain this operation in VC1.
You may not want to ask the shared loader about a particular operation, because it may stateless -- that is, it does not itself track the request operations. Consider, you want to use class Loader several times for starting network requests - possible in parallel. Then, which request do you mean when you ask one property of Loader which tells you something about the state of a request? (it won't work).
So, again back to a working approach and to VC1:
Suppose, in VC1 you obtained the RequestOperation object which is a subclass of NSOperation. Suppose, RequestOperation has a property responseBody - which is a NSData object representing the eventual response data of the request operation.
In order to obtain the eventual response body of the request, you cannot just ask the property: the connection could possibly still running - the you would get nil or garbage, or you might block the thread. The behavior is dependent on the implementation of RequestOperation.
The solution is as follows:
In VC2:
We assume, VC1 has "passed" the requestOp to VC2 (for example in prepareForSegue:sender:).
In order to retrieve the response body in an asynchronous correct manner, you need some extra steps:
Create a NSBlockOperation which executes a block which handles the response body, for example:
NSBlockOperation* handlerOp = [NSBlockOperation blockOperationWithBlock:^{
NSData* body = requestOp.responseBody;
dispatch_async(dispatch_get_main_queue(), ^{
self.model = body;
[self.tableView reloadData];
});
}];
Then, make the handlerOp dependent on the requestOp - that is, start executing handlerOp when requestOp finished:
[handlerOP addDependency:requestOp];
Add the handlerOp to a queue, in order to execute:
[[NSOperation mainQueue] addOperation:handlerOp];
This still requires you to think "asynchronously" - there is no way around this. The best is, to get used to the practical patterns and idioms.
An alternative approach is using RXPromise (from a third party library):
In VC1:
requestPromise = [Loader fetchWithURL:url];
Now, in VC2:
We assume, VC1 has "passed" the requestPromise to VC2 (for example in prepareForSegue:sender:).
For example in viewDidLoad:
requestPromise.thenOn(dispatch_get_main_queue(), ^id(id responseBody){
// executes on main thread!
self.model = responseBody;
[self.tableView reloadData];
return nil;
}, nil);
Bonus:
If required, you can cancel the network request at any time through sending cancel to the promise:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.requestPromise cancel];
self.requestPromise = nil;
}
I've figured it out. In my second view (where i w8 for the operation complete) I cannot w8 using ThreadSleep! I have to use [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

iOS block being stoped when view pushed [duplicate]

Earlier today I asked the following question: iOS block being stoped when view pushed
The operation I mentioned (OP1) is actually a "http get" to my server, using NSURLConnection.
After even more investigation I discovered that the block doesn't actually "die". What really happens is that the request is actually SENT (the server side logs it), even after the view is pushed (verified via [NSThread sleep:10]). The server responds but then NOTHING happens on the app side if the view2 has been pushed! almost as if the connection had lost its delegate! Another possibility im looking at is "the fact that NSURLConnection is on the rsMainLoop related?"
Can anyone help?
Pls don't forget that:
0. Everything works fine as long as the view2 is not pushed until operation completion.
1. The request is sent async
2. I set the delegate and it works as long as the view dont change
3. view1 starts the operation using the "singleton object reference" property "OP1Completed"
4. view2 checks the completion of OP1 via propertie on the "singleton object reference"
5. view2 gets the "result" by going to the "singleton.OP1Result" property
Edit 1:
Ok lets have some code. First here is the relevant code of my singleton (named "Interaction"):
-(void)loadAllContextsForUser:(NSString *)username{
userNameAux = username;
_loadingContextsCompleted = NO;
if (contextsLoaderQueue == NULL) {
contextsLoaderQueue = dispatch_queue_create("contextsLoaderQueue", NULL);
}
dispatch_async(contextsLoaderQueue, ^{
NSLog(#"Loading all contexts block started");
[self requestConnectivity];
dispatch_async(dispatch_get_main_queue(), ^{
[Util Get:[NSString stringWithFormat:#"%#/userContext?username=%#", Util.azureBaseUrl, [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
successBlock:^(NSData *data, id jsonData){
NSLog(#"Loading all contexts block succeeded");
if([userNameAux isEqualToString:username]){
_allContextsForCurrentUser = [[NSSet alloc]initWithArray: jsonData];
}
} errorBlock:^(NSError *error){
NSLog(#"%#",error);
} completeBlock:^{
NSLog(#"load all contexts for user async block completed.");
_loadingContextsCompleted = YES;
[self releaseConnectivity];
}];
});
while (!_loadingContextsCompleted) {
NSLog(#"loading all contexts block waiting.");
[NSThread sleepForTimeInterval:.5];
}
});
NSLog(#"Load All Contexts Dispatched. It should start at any moment if it not already.");
}
And here is the class Util, which actually handles the request/response
-(id)initGet:(NSString *)resourceURL successBlock:(successBlock_t)successBlock errorBlock:(errorBlock_t)errorBlock completeBlock:(completeBlock_t)completeBlock;{
if(self=[super init]){
_data=[[NSMutableData alloc]init];
}
_successBlock = [successBlock copy];
_completeBlock = [completeBlock copy];
_errorBlock = [errorBlock copy];
NSURL *url = [NSURL URLWithString:resourceURL];
NSMutableURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
//[_conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
//[_conn start];
NSLog(#"Request Started.");
return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
id key = [[jsonObjects allKeys] objectAtIndex:0];
id jsonResult = [jsonObjects objectForKey:key];
_successBlock(_data, jsonResult);
_completeBlock();
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
_errorBlock(error);
_completeBlock();
}
And finally here is the relevant part VC1 (pushing in VC2)
- (IBAction)loginClicked {
NSLog(#"login clicked. Preparing to exibit next view");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
AuthenticationViewController *viewController = (AuthenticationViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ContextSelectionView"];
NSLog(#"Preparation completed. pushing view now");
[self presentViewController:viewController animated:YES completion:nil];
}
You might be surprised, but there are a couple of solutions - some of which are very common and can be implemented very easily ;) Even though, this answer is ridiculous elaborate, the actual solution to your problem will not exceed a few lines of code. :)
You ran into a typical "async problem" - well, it's less than a problem, rather a typical programming task nowadays.
What you have is an asynchronous task, OP1. This will be started from within ViewController 1 (VC1), and at some indeterminate time later, it will eventually produce either a result or an error.
The eventual result of OP1 should be handled later in VC2.
There are a few approaches how a client can obtain the eventual result, for example: via KVO, delegate method, completion block, callback function, future or promise and per notification.
These approaches above have one property in common: the call-site gets notified by the asynchronous result provider (and not vice versa).
Polling for the result until it is available, is a bad approach. Likewse, hanging in a semaphore and blocking the current thread until the result is "signaled" is equally suboptimal.
You are probably familiar with completion blocks. A typical asynchronous method which notifies the call-site when the result is available looks like this:
typedef void (^completion_block_t)(id result);
- (void) doSomethingAsyncWithCompletion:(completion_block_t)completionHandler;
Note: the call-site provides the completion handler, while the async tasks calls the block when it is finished, and passes its result (or error) to the result parameter of the block. Unless otherwise stated, the execution context - that is the thread or dispatch queue or NSOperationQueue - of where the block will be executed is not known.
But when thinking about your problem, a simple async function and a completion handler doesn't yield a viable solution. You cannot pass that "method" easily from VC1 to VC2 and then later "attach" somehow a completion block in VC2.
Luckily, any asynchronous task can be encapsulated into an NSOperation. An NSOperation has a completion block as a property which can be set by the call-site or elsewhere. And an NSOperation object can be easily passed from VC1 to VC2. VC2 simply adds a completion block to the operation, and eventually gets notified when its finished and the result is available.
However, while this would be a viable solution for your problem - there are in fact a few issues with this approach - which I don't want to elaborate, but instead propose an even better one: "Promises".
A "Promise" represents the eventual result of an asynchronous task. That is, a promise will exist even though the result of the asynchronous task is not yet evaluated. A Promise is an ordinary object which you can send messages. Thus, Promises can be passed around much like NSOperations. A Promise is the return value of an asynchronous method/function:
-(Promise*) doSomethingAsync;
Don't mismatch a Promise with the asynchronous function/method/task/operation - the promise is just a representation of the eventual result of the task.
A Promise MUST be eventually resolved by the asynchronous task - that is, the task MUST send the promise a "fulfill" message along with the result value, or it MUST send the promise the "reject" message along with an error. The promise keeps a reference of that result value passed from the task.
A Promise can be resolved only once!
In order to obtain the eventual result a client can "register" a success handler and an error handler . The success handler will be called when the task fulfills the promise (that is, it was successful), and the error handler will be called when the task rejected the promise passing along the reason as an error object.
Assuming a particular implementation of a promise, resolving a promise may look like this:
- (Promise*) task {
Promise* promise = [Promise new];
dispatch_async(private_queue, ^{
...
if (success) {
[promise fulfillWithValue:result];
}
else {
NSError* error = ...;
[promise rejectWithReason:error];
}
});
return promise;
}
A client "registers" handlers for obtaining the eventual result as follows:
Promise* promise = [self fetchUsers];
promise.then( <success handler block>, <error handler block> );
The success handler and error handler block are declared as follows:
typedef id (^success_handler_block)(id result);
typedef id (^error_handler_block)(NSError* error);
In order to just "register" a success handler (for the case, the async tasks "returns" successfully) one would write:
promise.then(^id(id users) {
NSLog(#"Users:", users);
return nil;
}, nil);
If the task succeeds, the handler will be called - which prints the users to the console.
When the task fails, the success handler will not be called.
In order to just "register" an error handler (for the case, the async tasks fails) one would write:
promise.then(nil, ^id(NSError* error) {
NSLog(#"ERROR:", error);
return nil;
}, nil);
If the task succeeds, the error handler will not be called. Only if the task fails (or any children tasks), this error handler will be invoked.
When the result of the async task is eventually available, the code within the handlers will be executed "in some unspecified execution context". That means, it may execute on any thread. (Note: there are ways to specify the execution context, say the main thread).
A promise can register more than one handler pair. You can add as many handlers as you want, and where and when you want. Now, you should understand the connection with your actual problem:
You can start an asynchronous task in VC1, and get a promise. Then pass this promise to VC2. In VC2 you can add your handler, which will get invoked when the result is eventually available.
Don't worry when the result is actually already available when passing the promise to VC2, that is, when the promise has been resolved already. You can still add handlers and they get fired properly (immediately).
You can also "chain" multiple tasks - that is, invoke task2 once when task1 is finished. A "chain" or "continuation" of four async tasks looks as follows:
Promise* task4Promise =
[self task1]
.then(^id(id result1){
return [task2WithInput:result1];
}, nil)
.then(^id(id result2){
return [task3WithInput:result2];
}, nil)
.then(^id(id result3){
return [task4WithInput:result3];
}, nil);
task4Promise represents the eventual result of task4WithInput:.
One can also execute tasks in parallel, like taskB and taskC which will get started in parallel when taskA has been finished successfully:
Promise* root = [self taskA];
root.then(^id(id result){
return [self taskB];
}, nil);
root.then(^id(id result){
return [self taskC];
}, nil);
With this scheme, one can define an acyclic graph of tasks, where each is dependent on the successful execution of its successor ("parent"). "Errors" will be passed through to the root, and handled by the last error handler (if any).
There are a few implementations for Objective-C. I've written one myself: "RXPromise" (available on GitHub). One of the strongest feature is "Cancellation" - which is NOT a standard feature of promises, but implemented in RXPromise. With this, you can selectively cancel a tree of asynchronous tasks.
There is a lot more about promises. You may search the web, especially in the JavaScript community.
I'm not sure I understand the work flow that goes on in the first controller -- specifically, what the user does to initiate the download, and what else he does before the next controller gets presented (and when that controller gets instantiated). When I've made apps in the past that required doing downloads from multiple classes, I've created a download class that creates the NSURLConnection, and implements all the call backs. It has one delegate protocol method to send back the data (either raw data or error object) to its delegate.
I made a simple test case simulating what I think your work flow is, using two buttons. One instantiates a Downloader class instance, creates the next controller, sets it as the delegate of the downloader, and starts the download. The second button does the push to that second controller. This works, no matter when the push happens, but I don't know if it's relevant to your situation (I test using the Network Link Conditioner to simulate a slow connection).
The first Controller:
#import "ViewController.h"
#import "ReceivingViewController.h"
#import "Downloader.h"
#interface ViewController ()
#property (strong,nonatomic) ReceivingViewController *receiver;
#end
#implementation ViewController
-(IBAction)buttonClicked:(id)sender {
Downloader *loader = [Downloader new];
self.receiver = [self.storyboard instantiateViewControllerWithIdentifier:#"Receiver"];
loader.delegate = self.receiver;
[loader startLoad];
}
-(IBAction)goToReceiver:(id)sender {
[self.navigationController pushViewController:self.receiver animated:YES];
}
The Download class .h:
#protocol DownloadCompleted <NSObject>
-(void)downloadedFinished:(id) dataOrError;
#end
#interface Downloader : NSObject
#property (strong,nonatomic) NSMutableData *receivedData;
#property (weak,nonatomic) id <DownloadCompleted> delegate;
-(void)startLoad;
Downloader .m:
-(void)startLoad {
NSLog(#"start");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) self.receivedData = [NSMutableData new];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.receivedData.length = 0;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.delegate downloadedFinished:error];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.delegate downloadedFinished:self.receivedData];
}
-(void)dealloc {
NSLog(#"In Downloader dealloc. loader is: %#",self);
}
The second controller:
#interface ReceivingViewController ()
#property (strong,nonatomic) NSData *theData;
#end
#implementation ReceivingViewController
-(void)downloadedFinished:(id)dataOrError {
self.theData = (NSData *)dataOrError;
NSLog(#"%#",self.theData);
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"%#",self.theData);
}
So, here is what I think will work for sure:
Pass the flag to the new controller. If the flag is unfinished, then start over loading in the new VC and make sure none of the data shows up until it is done loading.
I do think it is weird that the thread stops though, with the new VC being pushed, because when I dispatch asynchronous calls with AFNetworking, it does continue to load even after a new VC is pushed. Perhaps if you are using a different framework, you should use AFNetworking.
So, if your thread actually does continue after the new VC is pushed on (as I suspect it does - you just think it doesn't keep going because it crashes the code), then try the following:
a) pass flag, if operation finished, proceed normally
b) if not, don't load anything and invoke some kind of delegate method between the two that checks if the flag is set, and returns the data if so.
If you have questions on how to set up a delegate, just ask and I can fill in some details on that.
As already mentioned in a comment in you first question: you have probably two issues:
A design problem
A code issue, causing the block. (but without code this is difficult to figure out).
Lets propose a practical approach:
Say, our singleton is some "Loader" class which performs HTTP requests. Instead of polling a property which determines the state of the network request, you should return some object which you can ask for the state, or even better where VC2 can register a completion block which gets called when the request is finished.
An NSOperation could be "used" to represent the eventual result of the asynchronous network request. But this is a bit unwieldy - suppose we have a subclass RequestOperation:
RequestOperation* requestOp = [[Loader sharedLoader] fetchWithURL:url];
Now, "requestOp" represents your network request, including the eventual result.
You can obtain this operation in VC1.
You may not want to ask the shared loader about a particular operation, because it may stateless -- that is, it does not itself track the request operations. Consider, you want to use class Loader several times for starting network requests - possible in parallel. Then, which request do you mean when you ask one property of Loader which tells you something about the state of a request? (it won't work).
So, again back to a working approach and to VC1:
Suppose, in VC1 you obtained the RequestOperation object which is a subclass of NSOperation. Suppose, RequestOperation has a property responseBody - which is a NSData object representing the eventual response data of the request operation.
In order to obtain the eventual response body of the request, you cannot just ask the property: the connection could possibly still running - the you would get nil or garbage, or you might block the thread. The behavior is dependent on the implementation of RequestOperation.
The solution is as follows:
In VC2:
We assume, VC1 has "passed" the requestOp to VC2 (for example in prepareForSegue:sender:).
In order to retrieve the response body in an asynchronous correct manner, you need some extra steps:
Create a NSBlockOperation which executes a block which handles the response body, for example:
NSBlockOperation* handlerOp = [NSBlockOperation blockOperationWithBlock:^{
NSData* body = requestOp.responseBody;
dispatch_async(dispatch_get_main_queue(), ^{
self.model = body;
[self.tableView reloadData];
});
}];
Then, make the handlerOp dependent on the requestOp - that is, start executing handlerOp when requestOp finished:
[handlerOP addDependency:requestOp];
Add the handlerOp to a queue, in order to execute:
[[NSOperation mainQueue] addOperation:handlerOp];
This still requires you to think "asynchronously" - there is no way around this. The best is, to get used to the practical patterns and idioms.
An alternative approach is using RXPromise (from a third party library):
In VC1:
requestPromise = [Loader fetchWithURL:url];
Now, in VC2:
We assume, VC1 has "passed" the requestPromise to VC2 (for example in prepareForSegue:sender:).
For example in viewDidLoad:
requestPromise.thenOn(dispatch_get_main_queue(), ^id(id responseBody){
// executes on main thread!
self.model = responseBody;
[self.tableView reloadData];
return nil;
}, nil);
Bonus:
If required, you can cancel the network request at any time through sending cancel to the promise:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.requestPromise cancel];
self.requestPromise = nil;
}
I've figured it out. In my second view (where i w8 for the operation complete) I cannot w8 using ThreadSleep! I have to use [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

Waiting for several networking async calls in iOS

From a view controller, as a result of a button action, I need to create a custom object that manages a set of asynchronous remote service calls, and call the method of such object that fires those service calls. I need the view controller to wait for all the async networking operations to have finished in order to update its view. Since the networking operations are async, I don't know how I'd communicate from the custom object managing this tasks to the view controller when all operations are done.
Here is the code I currently have. The code snippet in the view controller is like this (result var is not currently used):
- (void)loadData
{
BOOL __block result = NO;
dispatch_queue_t queue = dispatch_queue_create(dataLoadQueue, NULL);
dispatch_async(queue,^{
Loader *loader = [[Loader alloc] init];
[loader loadData];
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
// Update view and notify success
}
else {
// Update view and notify error
}
});
});
dispatch_release(queue);
}
And this is the loader custom object side:
- (void)loadData
{
if ([Reachability checkNetStatus]) {
Service1 *service1 = [[Service1 alloc] init];
[service1 callAsyncService];
Service2 *service2 = [[Service2 alloc] init];
[service2 callAsyncService];
// More service calls
}
else {
// Notify network not reachable
}
}
Objects service1, service2... serviceN conform the NSURLConnectionDelegate and I notify they have finished in its connectionDidFinishLoading: by means of the NSNotificationCenter (loader object is listening for such notifications). Then, I don´t know what is the correct way of making loader wait for all the networking operations, and notify back the view controller.
Thanks in advance
There are probably lots of ways you could do this. First, I don't think there's any need to use GCD in the view controller -- loader is already doing things asynchronously, so the creation of loader is fast.
As for how Loader knows when all its network operations are done, you could just keep a list of strings in a mutable array, like "1 done", "2 done", etc. that would be the same as strings sent in the user info of the notifications called in connectionDidFinishLoading:. All the services could send the same notification, but with different user info. In the selector for the observer, remove the string identical to the one in the user info, and check if the array is empty -- when it is, all your services are done. At that point, I would use a delegate method to pass back the data to the view controller. Something like this in Loader:
- (void)viewDidLoad {
[super viewDidLoad];
self.doneStrings = [#[#"1 done", #"2 done", #"3 done", #"4 done"] mutableCopy];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationReceived:) name:#"SeriveFinishedNotification" object:nil];
}
-(void)notificationReceived:(NSNotification *) aNote {
[self.doneStrings removeObjectIdenticalTo:[aNote.userInfo objectForKey:#"doneString"]];
if (self.doneStrings.count == 0)
[delegate doSomethingWithTheData: theData];
}
You would probably need to some other things like handle the case where some of the network operations fail.
If you want to wait until the async tasks were done, you can use a semaphore. See the example below, the logic is pretty simply. I think you can easily adapt to your case.
//create the semaphore
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[objectManager.HTTPClient deletePath:[address addressURL] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//some code here executed in background
dispatch_semaphore_signal(semaphore); //sends a notification to release the semaphore
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//some other code here also executed in background
dispatch_semaphore_signal(semaphore); //sends a notification to release the semaphore
}];
//holds the thread until the dispatch_semaphore_signal(semaphore) is send
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
You haven't shared the details of how these asynchronous requests work, but another approach is to make these asynchronous requests NSOperation objects that you submit to a NSOperationQueue. (AFNetworking is an example of this sort of implementation.) When you do that, you can create yet another NSOperation to be triggered upon the completion of the network request operations, by make it dependent upon those network request operations. Thus it will only run when all of the network requests are done. Using an NSOperation-based solution enjoys other benefits, too (e.g. you can use setMaxConcurrentOperationCount to let you enjoy concurrency, but not run too many concurrent requests at any given time).
References
Ray Wenderlich's How To Use NSOperations and NSOperationQueues
Defining a Custom Operation Object in Apple's Concurrency Programming Guide

Resources