Azre iOS客户端SDK中的乐观并发

时间:2017-03-09 05:33:11

标签: ios azure azure-mobile-services

我有一个关于Azure移动客户端SDK中的并发性的查询。

对于Windows我发现Conflict link用于处理并发,但我找不到相同的iOS客户端SDK。

任何人都可以建议或帮助如何在iOS客户端SDK中处理并发。

1 个答案:

答案 0 :(得分:1)

以下是处理与iOS SDK冲突的old Mobile Services page。从那时起,与iOS SDK脱机同步的设置没有改变。

1)设置MSSyncContextDelegate并在创建时将其传递给MSSyncContext构造函数。 2)在您的代理中实施tableOperation:(MSTableOperation *)operation onComplete:(MSSyncItemBlock)completion。执行操作后,检查MSErrorPreconditionFailed错误代码,并根据应用程序的需要决定该怎么做。

- (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);
        }
    }];
}