您好我需要从azure下载超过2000条记录,您当时可以下载的最大值为1000条,因此我需要使用完成处理程序来下载200条。
他们发布此代码作为示例,但我不知道如何使用。
如果我将此复制到Xcode,则会出现错误
在页面中返回数据
移动服务限制单个响应中返回的记录数量。要控制向用户显示的记录数,必须实现分页系统。通过使用MSQuery对象的以下三个属性来执行分页:
BOOL includeTotalCount
NSInteger fetchLimit
NSInteger fetchOffset
在下面的示例中,一个简单的函数从服务器请求20条记录,然后将它们附加到以前加载的记录的本地集合中:
- (bool) loadResults() {
MSQuery *query = [self.table query];
query.includeTotalCount = YES;
query.fetchLimit = 20;
query.fetchOffset = self.loadedItems.count;
[query readWithCompletion:(NSArray *items, NSInteger totalCount, NSError *error) {
if(!error) {
//add the items to our local copy
[self.loadedItems addObjectsFromArray:items];
//set a flag to keep track if there are any additional records we need to load
self.moreResults = (self.loadedItems.count < totalCount);
}
}];
}
感谢您的帮助。
答案 0 :(得分:4)
如果您收到Error " Expect Method Body "
,那么您会错误地将其复制到您的代码中,并且存在格式问题。
如果你想在一次通话中加载分页数据,我会这样做:
你的.h文件中的声明
typedef void (^CompletionBlock) ();
@property (nonatomic, strong) NSMutableArray *results;
你的.m文件中的
- (void)loadData
{
self.results = [[NSMutableArray alloc] init];
MSClient *client = [MSClient clientWithApplicationURLString:@"YOUR_URL" applicationKey:@"YOUR_KEY"]
MSTable *table = [client tableWithName:@"YOUR_TABLE"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"YOUR_SELECT_FILTER"];
MSQuery *query = [[MSQuery alloc] initWithTable:table predicate:predicate];
//note the predicate is optional. If you want all rows skip the predicate
[self loadDataRecursiveForQuery:query withCompletion:^{
//do whatever you need to do once the data load is complete
}];
}
- (void)loadDataRecursiveForQuery:(MSQuery *)query withCompletion:(CompletionBlock)completion
{
query.includeTotalCount = YES;
query.fetchLimit = 1000; //note: you can adjust this to whatever amount is optimum
query.fetchOffset = self.results.count;
[query readWithCompletion:(NSArray *items, NSInteger totalCount, NSError *error) {
if(!error) {
//add the items to our local copy
[self.results addObjectsFromArray:items];
if (totalCount > [results count]) {
[self loadDataRecursiveForQuery:query withCompletion:completion];
} else {
completion();
}
}
}];
}
注意:我没有测试过这段代码,但它应该或多或少有效。