ObjectiveCloudant 0.2.3

ObjectiveCloudant 0.2.3

测试已测试
语言语言 Obj-CObjective C
许可证 Apache 2
发布上次发布2016年9月

Rhys ShortMike RhodesTom Blench 维护。



  • IBM Cloudant

objective-cloudant

这个库已过时,已替换为 SwiftCloudant

应用程序使用 objective-cloudant 在 Cloudant 或 CouchDB 上存储、索引和查询远程 JSON 数据。

Objective-Cloudant 是一个 Apache CouchDB™ 客户端。它由 Cloudant 构建,并在 Apache 2.0 许可证 下提供。

早期发布版

这是该库的早期发布版,支持以下操作

  • 通过文档 ID 获取文档。
  • 更新和删除文档。
  • 创建 Cloudant 查询索引(JSON 和文本索引)。
  • 通过 Cloudant 查询查找文档。

我们将陆续在未来的版本中完善功能集。

在您的项目中使用

objective-cloudant 通过 CocoaPods 提供,要安装它请向您的 Podfile 添加以下行

pod ObjectiveCloudant

库概述

将库添加到项目后,添加和读取文档的基本方法如下

#import <ObjectiveCloudant/ObjectiveCloudant.h>

//Create a CDTCouchDBClient
NSURL *cloudantURL = [NSURL URLWithString:@"https://username.cloudant.com"];
CDTCouchDBClient *client = [CDTCouchDBClient clientForUrl:cloudantURL
                                                 username:@"username"
                                                 password:@"password"];

//access database
CDTDatabase *db = client[@"databasename"];

//create a document
[db putDocumentWithId:@"doc1"
                 body:@{@"hello":@"world"}
     completionHander:^(NSString *docId,
                        NSString *revId,
                        NSInteger statusCode,
                        NSError *operationError){
        if (operationError){
            NSLog(@"Error encountered while creating a document. Error: %@", error);
        } else {
            NSLog(@"Created document %@ with revision id %@", docId, revId);
        }

}];

//read a document
[db getDocumentWithId:@"doc1"
     completionHander:^(NSDictionary<NString*,NSObject*> *document,
                       NSError *operationError){
        if (operationError) {
            NSLog(@"Encountered an error while reading a document. Error:%@", error);
        } else {
            NSLog(@"Read document: %@",document);
        }
}];

//delete a document
[db deleteDocumentWithId:@"doc1"
           revisionId: @"1-revisionidhere"
    completionHandler: ^(NSInteger statusCode, error){
        if (error) {
            NSLog(@"Encountered an error while deleting a document. Error: %@", error);
        } else {
            NSLog(@"Document deleted");
        }
}];

在 Swift 中使用如下

import ObjectiveCloudant

// create a CDTCouchDBClient
let url = NSURL(string: "https://example.cloudant.com")!
let client = CDTCouchDBClient(forURL: url,
        username: "username",
        password: "password")!

//access database
let db = client["example"]!


// create document
db.putDocumentWithId("doc1",
    body: ["hello": "world"],
    completionHandler: { (docId, revId, statusCode, operationError) -> Void in
        if let error = operationError {
            print("Encountered an error creating document. Error: \(error)")
        } else {
            print("Created document \(docId), at revision \(revId)")
        }
})


//read document
db.getDocumentWithId("doc1",
    completionHander:(document, operationError) -> Void {
        if let error = operationError {
            println("Encountered an error reading a document. Error: \(error)")
        } else {
            println("Read document \(document!)")
        }
})

//delete document
db.deleteDocumentWithId("doc1",
    revisionId: "1-revisionidhere",
    completionHandler: { (statusCode, operationError) -> Void in
        if let error = operationError {
            print("Encountered error: \(error!)")
        } else {
            print("document deleted")
        }
})

查找数据

objective-cloudant 直接支持 Cloudant 查询 API

创建索引

// Create a JSON Index
CDTCreateQueryIndexOperation *op = [[CDTCreateQueryIndexOperation alloc] init];
op.indexType = CDTQueryIndexTypeJson;
op.fields = @[ @{@"foo":@"asc"}, @{@"bar" : @"desc"}]
op.createIndexCompletionBlock = ^(NSError * _Nullable error){
       if(error){
           NSLog(@"Index creation failed. Error %@",error);
       } else {
           NSLog(@"Index created");
       }
   };

[db addOperation:op];


// Create a text index
CDTCreateQueryIndexOperation *op = [[CDTCreateQueryIndexOperation alloc] init];
op.indexType = CDTQueryIndexTypeText;
op.fields = @[ @{ @"name" : @"foo", @"type" : @"string"}]
op.createIndexCompletionBlock = ^(NSError * _Nullable error){
       if(error){
           NSLog(@"Index creation failed. Error %@",error);
       } else {
           NSLog(@"Index created");
       }
   };

[db addOperation:op];
// Create a JSON index
let op = CDTCreateQueryIndexOperation()
op.indexType = .Json
op.fields = [["foo":"asc"],["bar":desc]]
op.createIndexCompletionBlock =  {(error) -> Void in
        if let _ = error {
            print("Index creation failed with error: \(error)")
        } else {
            print("Index created")
        }
}

db.addOperation(op)

// Create a text index
let op = CDTCreateQueryIndexOperation()
op.indexType = .Text
op.fields = [["name":"foo","type":"string"]]
op.createIndexCompletionBlock =  {(error) -> Void in
        if let _ = error {
            print("Index creation failed with error: \(error)")
        } else {
            print("Index created")
        }
}

db.addOperation(op)

查询文档

CDTQueryFindDocumentsOperation *op = [[CDTQueryFindDocumentsOperation alloc] init];
op.selector = @{@"foo": @"bar"};
op.documentFoundBlock = ^(NSDictionary * _Nonnull document){
    NSLog(@"Found document %@",document);
};
op.findDocumentCompletionBlock = ^(NSString * _Nullable bookmark, NSError* _Nullable error){
    if(error){
        NSLog(@"Failed to query database for documents");
    } else {
        NSLog(@"Query completed");
    }
};

[db addOperation:op];
let op = CDTQueryFindDocumentsOperation()
op.selector = ["foo":"bar"]
op.documentFoundBlock = {(document) -> Void in
    print("Found document \(document)")
}
op.findDocumentCompletionBlock = {(bookmark, error) -> Void in
    if let _ = error {
        print("Failed to query database for documents")
    } else {
        print("Query completed")
    }
}

db.addOperation(op)

删除索引

// Delete a JSON index
CDTDeleteQueryIndex *op = [[CDTDeleteQueryIndex alloc] init];
op.indexType = CDTQueryIndexTypeJson;
op.indexName = @"example";
op.desginDocName = @"exampleDesignDoc";
op.deleteIndexCompletionBlock = ^(NSInteger status, NSError * _Nullable error){
    if (error) {
        NSLog(@"Failed to delete index");
    } else {
        NSLog(@"Index deleted");
    }
};

[db addOperation:op];

// Delete a text index
CDTDeleteQueryIndex *op = [[CDTDeleteQueryIndex alloc] init];
op.indexType = CDTQueryIndexTypeText;
op.indexName = @"example";
op.desginDocName = @"exampleDesignDoc";
op.deleteIndexCompletionBlock = ^(NSInteger status, NSError * _Nullable error){
    if (error) {
        NSLog(@"Failed to delete index");
    } else {
        NSLog(@"Index deleted");
    };

[db addOperation:op];
// Delete a JSON index
let op = CDTDeleteQueryIndex()
op.indexType = .Json
op.indexName = "example"
op.designDocumentName = "exampleDesignDoc"
op.deleteIndexCompletionBlock = {(status, error) -> Void in
      if let _ = error {
          print("Failed to delete index")
      } else {
          print("Index deleted")
      }
}

db.addOperation(op)

// Delete a text index
let op = CDTDeleteQueryIndex()
op.indexType = .Text
op.indexName = "example"
op.designDocumentName = "exampleDesignDoc"
op.deleteIndexCompletionBlock = {(status, error) -> Void in
      if let _ = error {
          print("Failed to delete index")
      } else {
          print("Index deleted")
      }
}

db.addOperation(op)

要求

目前没有第三方依赖项。

贡献者

参见 CONTRIBUTORS

贡献于项目

请参见 CONTRIBUTING

许可协议

请参见 LICENSE