objective c - CouchDb views not updating -
okay i'm using couchdb v 1.2.0 scheme application objective-c. when run program i'm deleting old version of database , creating new 1 this:
couchdb *couchdb = [[couchdb alloc]init]; //deleting old version of db if exists xcode demo [couchdb deletedb:@"timevault" oncomplete:^(nsstring *message) { nslog(@"%@", message); }]; //creating new db [couchdb createdb:@"timevault" oncomplete:^(nsstring *message) { nslog(@"%@", message); }]; //creating views students , 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 db [couchdb createviewindb:views oncomplete:^(nsstring *message) { nslog(@"%@", message); }];
class implementation:
-(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 exists, no new db 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]]); } }
all of synchronous requests. for couchdb pros out there doesn't know obj-c, i'm saying this:
delete http://localhost:5984/database/ put http://localhost:5984/database/ put http://localhost:5984/database/_design/app, content-type: application/json
with body:
{"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); }}" }} }
my problem here view: http://localhost:5984/database/_design/app/_view/students
works half of time, other half it's not updating, i'm getting old id's, why this? i'm not looking @ old version of db because i'm creating new documents of students , courses , that's working. missing when i'm deleting db concerns _design document? googled around on can't find solves problem.
Comments
Post a Comment