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
}];
我不知道出了什么问题?
答案 0 :(得分: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);
}
}
});
}