Promises on the Example of PromiseKit in Objective-C

Promises on the Example of PromiseKit in Objective-C
Sooner or later every programmer encounters the problem of synchronous execution of certain actions. For example: get user information from the API, parse server response, save data to the database, update the view, and many others. To make it even more, at some of these stages you still need to deal with error handling. What should you do exactly?

Blocks

The first thing that comes to mind and that seems to be the most reasonable and popular solution is using blocks. What could such code look like?

// In this example, I omitted the implementation of these methods because they are the classic methods with blocks arguments

[self downloadUserInfo:^(id result, NSError *error) {
        if (!error) {
            [self parseUserInfo:result completion:^(NSDictionary *userInfo, NSError *error) {
                if (!error) {
                    [self saveUserInfoToDB:userInfo completion:^(BOOL success, NSError *error) {
                        if (!error) {
                            [self updateView];
                        }
                        else {
                            // do something with error
                        }
                    }];
                }
                else {
                    // do something with error
                }
            }];
        }
        else {
            // do something with error
        }
    }];

Although it is such a simple operation, we get quite a large piece of “spaghetti code”, which doesn’t look good. In such situations it is good to use “a promise”, which in our case means PromiseKit for Objective-C.

What our code will look like when we use PromiseKit?

- (PMKPromise *)downloadUserInfo
{
    return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        [session dataTaskWithURL:[NSURL URLWithString:@"adres_api.com"]
               completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
                   if (error) {
                       reject(error);
                   }
                   else {
                       fulfill(data);
                   }
               }];
    }];
}

- (PMKPromise *)parseUserInfo:(id)responseObject
{
    return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
        NSError *error = nil;
        NSDictionary *dictObj =
            [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        if (error) {
            reject(error);
        }
        else {
            fulfill(dictObj);
        }
    }];
}

- (PMKPromise *)saveUserInfoToDB:(NSDictionary *)userInfo
{
    return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        NSManagedObject *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context];
        [user setValue:userInfo[@"user_name"] forKey:@"userName"];
        // ...
        NSError *error = nil;
        [context save:&error];
        if (error) {
            reject(error);
        }
        else {
            fulfill(user.objectID);
        }
    }];
}

// The example of use definied earlier promises

[self downloadUserInfo]
        .then(^(id responseObject) {
            return [self parseUserInfo:responseObject];
        })
        .then(^(NSDictionary *userDict) {
            return [self saveUserInfoToDB:userDict];
        })
        .then(^(NSManagedObject *user) {
            [self updateView];
        })
        .catch(^(NSError *error) {
            NSLog(@"%@", error);
            // handle error
        })
        .finally(^{
            // turn off activity indicator etc.
        });

It’s obvious that the code is now clearer and more readable. Moreover, we can now handle all errors in one place. Of course, nothing stops you from putting “catch” after any “then”, in such a case an error thrown by “reject (error)” will be caught by a first “catch” encountered in a chain. Here, you can handle the error and, similarly to how it’s done in “then”, return another promise to continue calling individual “links” of the chain. The “finally” module is always placed and executed at the end (even if the “catch” doesn’t return anything). It doesn’t return any value, so it’s a good idea to use it to hide the progression of data loading or something similar.

The above example is only a part of what PromiseKit offers, so I encourage you to learn more about this library. It is well described on its official website: www.promisekit.org. In the second part I will present an example of “packaging” a delegation in Objective-C using PromiseKit.

 

Learn more

Classes and Structures in Swift

Swift language introduced a significant improvement of structures, and thus in many cases made them an interesting alternative to classes. Quite many functionalities of classes and structures are similar, but there are a few differences and because of them it is a good idea to consider which of these forms is more useful in what situation.
Read more

How to Send iOS Notifications in Different Languages? Guide to Dynamic Localization

The best way to set a connection between a user, provider, and an app? System notifications. They allow users to get the latest news in no time. This solution is easy to implement in apps dedicated to one market. But it gets complicated when the messages must be displayed in many languages. In such cases, the dynamic localization of remote notifications can be a real game-changer.
Read more

WebSockets on iOS – Real-time Communication That Doesn’t Slow Down the App

When you want to download data for the app, you probably use API RESTful interface. All it takes is to ask the server for the data and that's it! This method works well when the app doesn't need permanent access to new information. But what to do if the content has to be updated in real-time? A delay can make it impossible for the user to buy an item or make a sports bet, for example. But there’s a solution – WebSockets on iOS. Check out why you should implement them, and how to do it.
Read more

Project estimation

Check out how we use our knowledge in practice, and make your project with us.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in
M-COMMERCE category

17

client reviews

Clutch logo
Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

60+

projects in portfolio