您好,我需要从 azure 下载 2000 多条记录,当时最多可以下载 1000 条,所以我需要使用完成处理程序来下载 200 条记录。
他们发布了这段代码作为示例,但我不知道如何使用。
如果我将其复制到 Xcode,则会出现错误
- (bool)loadResults() - 错误“Expect Method Body”
返回页面数据
移动服务限制单个响应中返回的记录数量。要控制向用户显示的记录数量,您必须实现分页系统。分页是通过使用 MSQuery 对象的以下三个属性来执行的:
BOOL 包含TotalCount
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);
}
}];
}
感谢您的帮助。
最佳答案
如果您收到错误“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();
}
}
}];
}
注意:我还没有测试过这段代码,但它应该或多或少可以工作。
关于ios - Windows Azure 分页数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059615/