Gagan_iOS
Gagan_iOS

Reputation: 4080

Optimistic concurrency in Azre iOS client SDK

I have a query regarding concurrency in Azure mobile client SDK.

for windows I found Conflict link for handling concurrency but I could not found the same for iOS client SDK.

Can anyone please suggest or help how to handle concurrency in iOS client SDK.

Upvotes: 0

Views: 76

Answers (1)

Eric Hedstrom
Eric Hedstrom

Reputation: 1627

Here is the old Mobile Services page for handling conflicts with the iOS SDK. The setup for offline sync with the iOS SDK hasn't changed since then.

1) Set up an MSSyncContextDelegate and pass it to the MSSyncContext constructor when you create it. 2) Implement tableOperation:(MSTableOperation *)operation onComplete:(MSSyncItemBlock)completion in your delegate. After executing the operation, check for an MSErrorPreconditionFailed error code and decide what to do from there based on your app's needs.

- (void)tableOperation:(MSTableOperation *)operation onComplete:(MSSyncItemBlock)completion
{
    [operation executeWithCompletion:^(NSDictionary *item, NSError *error) {

        NSDictionary *serverItem = [error.userInfo objectForKey:MSErrorServerItemKey];

        if (error.code == MSErrorPreconditionFailed) {
            QSUIAlertViewWithBlock *alert = [[QSUIAlertViewWithBlock alloc] initWithCallback:^(NSInteger buttonIndex) {
                if (buttonIndex == 1) { // Client
                    NSMutableDictionary *adjustedItem = [operation.item mutableCopy];

                    [adjustedItem setValue:[serverItem objectForKey:MSSystemColumnVersion] forKey:MSSystemColumnVersion];
                    operation.item = adjustedItem;

                    [self doOperation:operation complete:completion];
                    return;

                } else if (buttonIndex == 2) { // Server
                    NSDictionary *serverItem = [error.userInfo objectForKey:MSErrorServerItemKey];
                    completion(serverItem, nil);
                } else { // Cancel
                    [operation cancelPush];
                    completion(nil, error);
                }
            }];

            NSString *message = [NSString stringWithFormat:@"Client value: %@\nServer value: %@", operation.item[@"text"], serverItem[@"text"]];

            [alert showAlertWithTitle:@"Server Conflict"
                              message:message
                    cancelButtonTitle:@"Cancel"
                    otherButtonTitles:[NSArray arrayWithObjects:@"Use Client", @"Use Server", nil]];
        } else {
            completion(item, error);
        }
    }];
}

Upvotes: 1

Related Questions