我在我的 iOS 应用程序中使用 cloud ant Db,名称为 tasks_master
包含近 1000 个文档。当我尝试从云 Ant 中获取数据时,它需要将近 30 秒才能获取我尝试使用以下代码的数据。
- (NSURL*) replicatorURL {
AppDelegate *app = [[UIApplication sharedApplication]delegate];
NSString *db_name = @"tasks_master";
NSString *url = [NSString stringWithFormat:@"https://%@:%@@%@.cloudant.com/%@",
username,
password,
username,db_name];
return [NSURL URLWithString:url];
}
- (void) sync:(UIViewController *)sender {
[self pullReplication:sender];
[self pushReplication:sender];
}
-(void)pullReplication:(UIViewController *)sender {
[self log:@"Starting pull replication"];
NSURL *url = [self replicatorURL];
AppDelegate *delegate1 = (AppDelegate *)[[UIApplication sharedApplication] delegate];
CDTReplicatorFactory *factory = delegate1.replicatorFactory;
CDTReplicator *replicator = [factory onewaySourceURI:url targetDatastore:delegate1.datastore];
[self startAndFollowReplicator:replicator label:@"pull"];
}
- (void) pushReplication:(UIViewController *)sender {
[self log:@"Starting push replication"];
NSURL *url = [self replicatorURL];
AppDelegate *delegate1 = (AppDelegate *)[[UIApplication sharedApplication] delegate];
CDTReplicatorFactory *factory = delegate1.replicatorFactory;
CDTReplicator *replicator = [factory onewaySourceDatastore:delegate1.datastore targetURI:url];
[self startAndFollowReplicator:replicator label:@"push"];
}
当我调用任务时
-(void)fetchtasks{
[[[CDTTodoReplicator alloc]init]sync];
self.indexManager = [[CDTIndexManager alloc] initWithDatastore:self.datastore
error:&error];
indexName= [self.datastore ensureIndexed:@[@"originator",@"members",@"meeting_status"] withName:@"meeting_index"];
query=@{@"$and":@[@{@"meeting_status":@"Scheduled"}, @{@"$or":@[@{@"originator":app.userName},@{@"members":app.userName}]}]};
result = [self.datastore find:query];
}
任何人都可以帮助我如何更快地获取数据。
最佳答案
我写了一个小助手类来减少这种事情的一些样板。这是上面引用的视频中使用的那个。
Cloudant.h https://gist.github.com/xpqz/f8b304353080c3963a45
Cloudant.m https://gist.github.com/xpqz/62b5267c250f04c30f9b
当您将文档添加到 Cloudant 数据库时,它们会通过所谓的主索引(也称为所有文档)变得可用。这可以通过命令行中的“curl”轻松检查,例如:
% curl https://skruger.cloudant.com/routes/_all_docs
您存储的每个文档都将出现在主索引返回的数据中。
设计文档有不同的目的——它们定义二级索引,称为 View ,主要编写为小的 javascript 函数,定义 map-reduce 操作以公开数据的某些方面。 View 允许您以免费获得的文档 ID 的其他方式索引您的文档。
您可以通过自己创 build 计文档直接创建自己的 View ,但访问数据库的客户端软件有时也会自动创 build 计文档和 View ,当您看到 _design 下出现您未明确创建的内容时,有时会感到困惑。
总之:每个文档都出现在主索引 _all_docs 中。设计文档用于保存用于生成二级索引的 javascript 函数,称为 View 。这些可以显式创建,也可以由客户端软件“在您背后”自动生成。
更多信息:
斯特凡
关于ios - 无法在 cloudant DB 中更快地获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29675550/