1

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

我不知道怎么了?

4

1 回答 1

3

只要您在表的读取操作中没有任何自定义脚本,它就应该可以工作。例如,如果您创建一个新表(称为“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参数的请求时返回的响应。现在,如果我们更改读取脚本以更改不是该格式的内容(例如,通过仅返回项目(而不是具有总计数属性的对象):

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()(不带参数)-将发送读取操作的(修改后的)结果;或者,如果您想对结果进行更广泛的更改,您可以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);
            }
        }
    });
}
于 2013-10-12T17:59:20.710 回答