ObjCradle

Created: 2012-03-25 15:25
Updated: 2014-01-12 23:14

README.md

ObjCradle

A thin Objective C wrapper around CouchDB's REST api

Introduction

Inspired by the node.js cradle and DotCradle librares. The CouchDB server is installed and configured using Couchbase Mobile and CouchCocoa.

I have been developing this as I create a prototype app to use an embedded CouchDB instance with pre-populated data. The embedded instance is then updated by (continuous) replication from a remote instance, updated by a simple CMS.

Dependancies

Road Map

  • Configuration to use a Remote Server
  • Configuration to install a blank db on first run

API Examples

###Installing an embedded database from mydatabase.couch file resource

	
ObjCradle *couch = [[ObjCradle default] initWithDB:@"mydatabase"];
[couch.server start: ^{  // ... this block runs later on when the server has started up:
    if (couch.server.error) {
        NSLog(@"Error staring CouchEmbeddedServer: %@", couch.server.error.domain);
        return;
    }
    couch.database = [couch.server databaseNamed:dbName];
    NSLog(@"CouchDB installed with ObjCradle on %@", couch.server.URL);
    ...
}];

###Retrieving all documents

	
ASIHTTPRequest *request= [[ObjCradle default] get:@"_all_docs"];
NSLog(@"Results: %@", request.responseString);

###Retrieving documents from a view using a key

ASIHTTPRequest *request = [[ObjCradle default] get:@"_design/category/_view/byParent" usingKey:[NSString stringWithFormat:@"%d", parent]];
NSLog(@"Results: %@", request.responseString);

###Replication Three replication types are supported ServerToClient, ClientToServer and BiDirectional. Continuous replication can be used.

[[ObjCradle default] replicate:@"http://me.iriscouch.com/mydatabase" replicationType:ServerToClient continous:YES];

###Selecting results as a custom class The results method returns a NSArray of NSDictionary instances. These can be exposed as a custom type:

######Item.h

@interface Item : NSObject {
        NSString *__id;
        NSString *__rev;
        NSString *_title;
        NSString *_summary;
        NSString *_body;
        NSString *_type;
        NSString *_category_id;
}

@property (nonatomic, retain) NSString *_id;
@property (nonatomic, retain) NSString *_rev;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *summary;
@property (nonatomic, retain) NSString *body;
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) NSString *category_id;

- (id)initWithProperties:(NSDictionary *)properties;


@end

######Item.m

#import "Item.h"

@implementation Item

@synthesize _id = __id, title = _title, summary = _summary, body = _body, _rev = __rev, type = _type, category_id = _category_id;

- (id)initWithProperties:(NSDictionary *)properties {
  [self setValuesForKeysWithDictionary:properties];
   return self;
}

@end
-(void) setItemId:(NSString *)itemId{
    ASIHTTPRequest *itemRequest = [[ObjCradle default] get:@"_design/item/_view/all" usingKey:[NSString stringWithFormat:@"%@", itemId]];
    NSDictionary *result = [[[ObjCradle default] results:itemRequest] objectAtIndex:0];
    _item = [[Item alloc] initWithProperties:result];
}

###Using the ASIHTTPRequestDelegate:

######ItemsController.h

#import "ASIHTTPRequestDelegate.h"

@interface ItemsController: NSObject <ASIHTTPRequestDelegate>

@end

######ItemsController.m

#import "ItemsController.h"
#import "ASIHTTPRequest.h"

@implementation ItemsController {
}

-(void)createModel {
    ASIHTTPRequest *itemRequest = [[ObjCradle default] get:@"_design/item/_view/all" usingKey:[NSString stringWithFormat:@"%@", itemId] requestDelegate:self];   
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    if (request.responseStatusCode == 200 || request.responseStatusCode == 201) {
        NSDictionary *result = [[[ObjCradle default] results:itemRequest] objectAtIndex:0];
        _item = [[Item alloc] initWithProperties:result];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"Request failed: %@", request.error)
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more