好的,我目前正在使用CouchDb v 1.2.0进行带有objective-c的方案应用程序。当我运行我的程序时,我正在删除旧版本的数据库并创建一个像这样的新版本:
CouchDb *couchDb = [[CouchDb alloc]init];
//Deleting the old version of the db if it exists for xcode demo
[couchDb deleteDb:@"timevault" onComplete:^(NSString *message) {
NSLog(@"%@", message);
}];
//Creating a new db
[couchDb createDb:@"timevault" onComplete:^(NSString *message) {
NSLog(@"%@", message);
}];
//Creating views to get students and courses
NSString *viewCourses = @"function (doc) { if (doc.type === \"Course\") { emit(doc._id, doc); } }";
NSString *viewStudents = @"function (doc) { if (doc.type === \"Student\") { emit(doc._id, doc); } }";
NSDictionary *views = @{@"views": @{@"courses": @{@"map":viewCourses}, @"students": @{@"map":viewStudents}}};
//Saving design document to db
[couchDb createViewInDb:views onComplete:^(NSString *message) {
NSLog(@"%@", message);
}];
班级实施:
-(void)deleteDb:(NSString *)name onComplete:(message)message{
NSMutableString *url = [[NSMutableString alloc]initWithString:@"http://localhost:5984/"];
[url appendString:name];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"DELETE"];
NSURLResponse *responseCode = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)responseCode;
NSNumber *responseStatusCode = [[NSNumber alloc] initWithLong:[httpResponse statusCode]];
if ([responseStatusCode isEqualToNumber:[NSNumber numberWithInt:200]]) {
message([NSString stringWithFormat:@"Old db %@ deleted", name]);
}
}else {
message([NSString stringWithFormat:@"Error: %@", [error localizedDescription]]);
}
}
-(void)createDb:(NSString *)name onComplete:(message)message{
NSMutableString *url = [[NSMutableString alloc]initWithString:@"http://localhost:5984/"];
[url appendString:name];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"PUT"];
NSURLResponse *responseCode = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)responseCode;
NSNumber *responseStatusCode = [[NSNumber alloc] initWithLong:[httpResponse statusCode]];
if ([responseStatusCode isEqualToNumber:[NSNumber numberWithInt:412]]) {
message(@"Db already exists, no new Db was created");
}else if ([responseStatusCode isEqualToNumber:[NSNumber numberWithInt:201]]){
message([NSString stringWithFormat:@"Db %@ created", name]);
}
}else {
message([NSString stringWithFormat:@"Error: %@", [error localizedDescription]]);
}
}
-(void)createViewInDb:(NSDictionary *)view onComplete:(message)message{
NSMutableString *url = [[NSMutableString alloc]initWithString:URL];
[url appendString:@"/_design/app"];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"PUT"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
NSData *asJson = [NSJSONSerialization dataWithJSONObject:view options:NSUTF8StringEncoding error:nil];
[request setHTTPBody:asJson];
NSURLResponse *responseCode = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if (!error){
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)responseCode;
NSNumber *responseStatusCode = [[NSNumber alloc] initWithLong:[httpResponse statusCode]];
if ([responseStatusCode isEqualToNumber:[NSNumber numberWithInt:201]]) {
message(@"Views in Db created");
}
}else {
message([NSString stringWithFormat:@"Error: %@", [error localizedDescription]]);
}
}
所有这些都是同步请求。 对于那些不知道obj-c的couchdb专业人士,我基本上是这样说的:
DELETE http://localhost:5984/database/
PUT http://localhost:5984/database/
PUT http://localhost:5984/database/_design/app,
content-type: application/json
身体:
{"views":
{
"courses": {
"map": "function (doc) {
if (doc.type === \"Course\") {
emit(doc._id, doc);
}}"
},
"students": {
"map": "function (doc) {
if (doc.type === \"Student\") {
emit(doc._id, doc);
}}"
}}
}
我的问题是我的观点: http://localhost:5984/database/_design/app/_view/students
只有一半时间有效,另一半没有更新,我老了id,为什么会这样?
我不是在看旧版本的数据库,因为我正在创建学生和课程的新文档,而且这些工作正常。当我删除与_design文档有关的数据库时,我错过了什么?我在谷歌搜索,但找不到任何解决我的问题的东西。