includeTotalCount
指示Windows Azure移动服务是否还应将服务器上的项目总数(而不只是返回的项目数)包括在查询结果中。

但totalCount始终为-1,
这是我的代码

MSQuery *query = [_skinsTable query];
query.predicate = bPredicate;
query.includeTotalCount = YES;
query.fetchOffset = offset;
query.fetchLimit = limit;
[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) {

    //here everything is OK(items, error), but totalCount is -1
}];

我不知道怎么了

最佳答案

只要您对表的读取操作中没有任何自定义脚本,它就可以正常工作。例如,如果创建一个新表(称为“test”),并在下面运行此代码,则其totalCount参数将具有适当的值(在本例中为4)。

- (IBAction)clickMe:(id)sender {
    MSClient *client = [MSClient clientWithApplicationURLString:@"https://YOUR-SERVICE.azure-mobile.net/"
                                                 applicationKey:@"YOUR-KEY"];
    MSTable *table = [client tableWithName:@"test"];
    NSDictionary *item1 = @{@"name":@"Scooby Doo",@"age":@10};
    NSDictionary *item2 = @{@"name":@"Shaggy",@"age":@19};
    NSDictionary *item3 = @{@"name":@"Daphne",@"age":@18};
    NSDictionary *item4 = @{@"name":@"Fred",@"age":@20};
    NSDictionary *item5 = @{@"name":@"Velma",@"age":@21};
    [table insert:item1 completion:^(NSDictionary *item, NSError *error) {
        [table insert:item2 completion:^(NSDictionary *item, NSError *error) {
            [table insert:item3 completion:^(NSDictionary *item, NSError *error) {
                [table insert:item4 completion:^(NSDictionary *item, NSError *error) {
                    [table insert:item5 completion:^(NSDictionary *item, NSError *error) {
                        MSQuery *query = [table query];
                        NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"age > 15"];
                        query.predicate = bPredicate;
                        query.includeTotalCount = YES;
                        query.fetchLimit = 3;
                        query.fetchOffset = 0;
                        [query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) {
                            NSLog(@"Items: %@", items);
                            NSLog(@"TotalCount: %d", totalCount);
                        }];
                    }];
                }];
            }];
        }];
    }];
}

检查请求和响应是否是使inlineCount正常工作的一种好方法是使用自定义过滤器来记录请求和响应。在下面定义一个新接口:
@interface FPLoggingFilter : NSObject<MSFilter>

@end

@implementation FPLoggingFilter

- (void)handleRequest:(NSURLRequest *)request next:(MSFilterNextBlock)next response:(MSFilterResponseBlock)response {
    NSLog(@"Request: %@", request);
    next(request, ^(NSHTTPURLResponse *resp, NSData *data, NSError *error) {
        NSLog(@"Response: %@", response);
        NSString *respBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Response body: %@", respBody);
        response(resp, data, error);
    });
}

@end

并更改最内层代码中的代码以使用该代码:
MSClient *filteredClient = [client clientWithFilter:[FPLoggingFilter new]];
MSTable *filteredTable = [filteredClient tableWithName:@"test"];
MSQuery *query = [filteredTable query];
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"age > 15"];
query.predicate = bPredicate;
query.includeTotalCount = YES;
query.fetchLimit = 3;
query.fetchOffset = 0;
[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) {
    NSLog(@"Items: %@", items);
    NSLog(@"TotalCount: %d", totalCount);
}];

现在,如果运行此代码,您应该在日志中看到该请求具有$inlinecount=allpages查询字符串参数。响应不是简单的对象数组。相反,它是一个具有两个属性的对象:结果和计数:
{
    "results" : [
        {"id":2,"age":19,"name":"Shaggy"},
        {"id":3,"age":18,"name":"Daphne"},
        {"id":4,"age":20,"name":"Fred"}
    ],
    "count":4
}

这是表在收到带有$inlinecount=allpages参数的请求时没有任何自定义脚本(或脚本未覆盖响应)时返回的响应。现在,如果我们更改读取脚本以更改非该格式的内容(例如,仅返回项目(而不返回具有total count属性的对象)):
function read(query, user, request) {
    var result = [];
    result.push({ id: 1, name: 'Scooby Doo', age: 10 });
    result.push({ id: 1, name: 'Shaggy', age: 19 });
    request.respond(200, results);
}

这样,客户端将无法检索该值,仅是因为HTTP响应中不存在该值。

在评论后更新:如果您想要在发送结果到客户端之前浏览结果并更改它们,同时仍然保留includeTotalCount功能,则有两个选择:要么直接更改结果数组,然后调用request.respond()(不带参数)-将发送读取操作的(修改的)结果;或者,如果您想对结果进行更广泛的更改,则可以从results数组中获取totalCount属性(是的,一个数字属性将附加到该数组;毕竟,这是JavaScript,所有地方都可以使用),如果$inlinecount=allpages参数已在请求的查询字符串中发送。使用该值,您可以适当地设置响应的格式,如下所示。
function read(query, user, request) {
    request.execute({
        success: function(results) {
            results.forEach(function(result, index) {
                result.newValue = index;
            });

            //request.respond(); - this should work.

            var totalCount = results.totalCount;
            if (totalCount) {
                request.respond(200, { count: totalCount, results: results });
            } else {
                request.respond(200, results);
            }
        }
    });
}

关于ios - MSQuery includeTotalCount不起作用吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19298781/

10-10 01:54