A
A
Artem_AK2015-06-30 17:08:12
Objective-C
Artem_AK, 2015-06-30 17:08:12

What are the ways to get data from the global queue besides the Notification center?

For example, in order for the controller to receive data by calling the model's "getData" method (shown below), I use the Notification center, which publishes the received data in the notification to which the given controller is subscribed.
I would like to know: are there any alternative options for getting data by the controller from the global queue?
Thank you!

- (void)getData {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"%@", error.description);
        } else {
            NSError *parsingError;
            NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsingError];
            if (parsingError) {
                    NSLog(@"Error while parsing data %@", parsingError.description);
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    ;
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"%@", error.description);
        } else {
            NSError *parsingError;
            NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsingError];
            if (parsingError) {
                    NSLog(@"Error while parsing data %@", parsingError.description);
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (completionBlock){
                       completionBlock(parsedData);
                   }
                });
            }
        }
    }];
    [task resume];
}

The call looks like this
[self getData:^(id result){
    //process
}];

Yes, you, in fact, use this method yourself in NSURLSession. In order to avoid callback hell, you can look towards PromiseKit
Another, almost forgotten way, through delegates...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question