1

好的,我目前正在将 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 文档有关的数据库时,我是否遗漏了什么?我对此进行了谷歌搜索,但找不到任何解决我问题的方法。

4

0 回答 0