0

My problem is as follows,

I have 4 class as follows,

A) UIviewController (UI) B) Static Method web service request class c) Static Method web service response parsing class D) Static Method web service response processing class

Flow of data is class A->B->C->D and from D class to again A ,

* required thing is B,C,D are in background process

Please let me know how to write source code to resolve this problem?

Please fill free to give any other alternative method solution for this problem.

Thanks in Advance.

Sandeep
  • 766
  • 5
  • 16

3 Answers3

1

You can use NSNotificationCenter service for callback. When you are done with process in Class D send notification. In Class A add/remove a observer

- (void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

[[NSNotificationCenter defaultCenter] addObserverForName:@"SyncCompleted" object:nil queue:nil usingBlock:^(NSNotification *note) {

    [self loadRecordsFromCoreData];// Do smoething
}];
}

- (void)viewDidDisappear:(BOOL)animated {

[super viewDidDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SyncCompleted" object:nil];
}

In Class D when the process completes call

 [[NSNotificationCenter defaultCenter] postNotificationName:@"SyncCompleted"
     object:nil];
Gyanendra Singh
  • 1,483
  • 12
  • 15
1

It's very hard to understand what you're asking, but with iOS, when you're doing most web services-type work, I tend to just recommend using AFNetworking rather than rolling your own: https://github.com/AFNetworking/AFNetworking

It handles most of the things you'll generally care about, like downloading and doing delegate processing outside of the main NSOperationQueue, etc.

Alternatively, if you only care about iOS7, the new NSURLSession class also makes these things easy: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html

But you should rarely need to roll your own.

ksimons
  • 3,797
  • 17
  • 17
1

Since at least the network operations are inherently asynchronous, your problem can be viewed as a "common asynchronous pattern":

When you want to execute asynchronous operation B immediately after asynchronous operation A has been finished, this is called "continuation".

There are a few approaches how one can implement continuation. This is dependent on how the asynchronous operation signals "completion" or "error" to the call-site.

Suppose the asynchronous operation is wrapped into a method which has a parameter completion which is a block. In this case, the asynchronous method signals completion through calling the caller-provided completion block, and passing the result as a parameter to the block:

typedef void (^completion_t)(id result);

- (void) doSomethingAsync:(completion_t)completionHandler;

call site:

[self doSomethingAsync:^(id result){/* now, result is available */});

Note, that the completion handler will be provided by the call-site, and that the completion handler will be called by the asynchronous operation once the result has been evaluated.

Now, in order to invoke a second asynchronous operation which gets invoked immediately after the first has been completed you can simply invoke it in the completion handler of the first:

Suppose the second asynchronous method requires an input, which must be provided by the call-site in parameter input:

- (void) doSomethingElseWithParam:(id)input 
                       completion:(completion_t)completionHandler;

Suppose the second asynchronous method takes the previously evaluated result as input. Here's the call site invoking asynchronous method doSomethingAsync:, and when completed it invokes doSomethingElseWithParam:completion:. Finally, when the second asynchronous method finished, the result will be printed to the console:

[self doSomethingAsync:^(id result){
    [self doSomethingElseWithParam:result completion:^(id finalResult) {
        NSLog(@"Final result: %@", finalResult);
    }]
});

"Chaining" asynchronous methods in such a way is also called "continuation".

Of course, a real implementation would handle all the error checks.

There are other ways to implement this. This was an example with completion blocks. One disadvantage with this approach is, that it becomes difficult to impossible to implement cancellation.

Another approach is using NSOperation's dependency feature (see NSOperation's addDependency: method (NSOperation Class Reference). NSOperation objects can be easily cancelled.

And yet another approach utilizes a Promise (see wiki: Futures and Promises) which represents the eventual result of an asynchronous operation. (There are a few Objective-C third party libraries which implement a Promise).

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67