Created
February 26, 2015 09:47
-
-
Save SeanWangDev/5cca644c805a5c247490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*========================================= h file =========================================*/ | |
#import <CouchbaseLite/CouchbaseLite.h> | |
@interface DemoQueue : CBLModel | |
/** Query for for queue based with queue_id. */ | |
+ (CBLQuery*) queryDemoQueueInDatabase: (CBLDatabase*)db forDemoQueueId: (NSString*)DQueueId; | |
/** Get an existing queue using queue_id, if no queue object exist, create it. */ | |
+ (instancetype) getDemoQueueInDatabase: (CBLDatabase*)db forDemoQueueId: (NSString*)DQueueId; | |
/** Push a string to queue */ | |
- (void) pushString:(NSString *)string; | |
/** Pop a string from queue */ | |
- (NSString *) popString; | |
/** The queue */ | |
@property (readwrite) NSArray *queue; | |
@end | |
/*========================================= m file =========================================*/ | |
#import "DemoQueue.h" | |
#define kDemoQueueDocType @"demoqueue" | |
@implementation DemoQueue | |
@dynamic queue; | |
/** Query for for queue based with queue_id. */ | |
+ (CBLQuery*) queryDemoQueueInDatabase: (CBLDatabase*)db forDemoQueueId: (NSString*)DQueueId { | |
CBLView *view = [db viewNamed:@"demoqueueview"]; | |
if (!view.mapBlock) { | |
[view setMapBlock: MAPBLOCK({ | |
if ([doc[@"type"] isEqualToString:kDemoQueueDocType] //type as annotation | |
) | |
{ | |
emit(@[doc[@"_id"]],doc[@"queue"]); | |
} | |
}) reduceBlock: nil version: @"1"]; | |
} | |
CBLQuery *query = [view createQuery]; | |
// query.endKey = query.startKey = @[DQueueId]; | |
return query; | |
} | |
/** Get an existing queue using queue_id, if no queue object exist, create it. */ | |
+ (instancetype) getDemoQueueInDatabase: (CBLDatabase*)db forDemoQueueId: (NSString*)DQueueId { | |
NSParameterAssert(DQueueId); | |
DemoQueue *dq; | |
NSString* DQDocId = DQueueId; | |
CBLDocument *doc; | |
if (DQDocId.length > 0) | |
doc = [db existingDocumentWithID: DQDocId]; | |
if (doc == nil) { | |
NSLog(@"Meeting session doc with %@ does not exist, creating a DemoQueue instance with empty array.", DQueueId); | |
doc = [db documentWithID:DQDocId]; | |
dq = [DemoQueue modelForDocument:doc]; | |
dq.type = kDemoQueueDocType; | |
dq.queue = nil; | |
} else { | |
dq = [DemoQueue modelForDocument:doc]; | |
} | |
return dq; | |
} | |
/** Push a string to queue */ | |
- (void) pushString:(NSString *)string { | |
NSParameterAssert(string); | |
NSMutableArray *newArray = [[NSMutableArray alloc]init]; | |
if (self.queue && [self.queue count]!=0) { //if queue is not empty, transfer to NSMutableArray for manipulation. | |
[newArray addObjectsFromArray:self.queue]; | |
} | |
[newArray addObject:string]; | |
self.queue = newArray; | |
} | |
/** Pop a string from queue */ | |
- (NSString *) popString { | |
if (self.queue == nil || self.queue.count == 0) { //if queue is empty, don't modify it and return. | |
NSLog(@"DemoQueue is empty"); | |
return nil; | |
} | |
//if queue is not empty, transfer to NSMutableArray for manipulation | |
NSMutableArray *newArray = [[NSMutableArray alloc] init]; | |
NSString *firstString = self.queue[0]; | |
[newArray addObjectsFromArray:self.queue]; | |
[newArray removeObjectAtIndex:0]; | |
self.queue = newArray; | |
return firstString; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*========================================= h file =========================================*/ | |
#import "ViewController.h" | |
@interface DemoViewController : ViewController | |
@end | |
/*========================================= m file =========================================*/ | |
#import "DemoViewController.h" | |
#import "AppDelegate.h" | |
#import "DemoQueue.h" | |
#define kDemoQueueId @"demoqueue" | |
@interface DemoViewController () | |
@property (weak, nonatomic) IBOutlet UILabel *QueueLabel; | |
@property (strong, nonatomic) CBLDatabase *database; | |
@property (strong, nonatomic) CBLLiveQuery *queueLiveQuery; | |
@end | |
@implementation DemoViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
[self setup]; | |
} | |
- (void)setup { | |
AppDelegate *app = [[UIApplication sharedApplication] delegate]; | |
self.database = app.database; | |
if (self.database != nil) { | |
self.queueLiveQuery = [DemoQueue queryDemoQueueInDatabase:self.database forDemoQueueId:kDemoQueueId].asLiveQuery; | |
[self.queueLiveQuery addObserver: self forKeyPath: @"demoqueue" | |
options: 0 context: NULL]; | |
[self.queueLiveQuery start]; | |
} else { | |
} | |
} | |
- (IBAction)pushToQueue:(id)sender { | |
DemoQueue* demoQueue = [DemoQueue getDemoQueueInDatabase:_database forDemoQueueId:kDemoQueueId]; | |
[demoQueue pushString:[self randomString]]; | |
NSError *error; | |
if ([demoQueue save:&error]) { | |
//do nothing | |
} else { | |
NSLog(@"Cannot udpate demo queue: %@", [error description]); | |
} | |
} | |
- (IBAction)popFromQueue:(id)sender { | |
DemoQueue* demoQueue = [DemoQueue getDemoQueueInDatabase:_database forDemoQueueId:kDemoQueueId]; | |
[demoQueue popString]; | |
NSError *error; | |
if ([demoQueue save:&error]) { | |
//do nothing | |
} else { | |
NSLog(@"Cannot udpate demo queue: %@", [error description]); | |
} | |
} | |
- (IBAction)refreshLiveQuery:(id)sender { | |
NSError *error; | |
CBLQueryEnumerator* result = [self.queueLiveQuery run: &error]; | |
for (CBLQueryRow* row in result) { | |
NSLog(@"found a row for demo queue, queue_id: %@, queue: %@", row.key, row.value); | |
self.QueueLabel.text = [(NSArray *)row.value componentsJoinedByString:@","]; | |
} | |
} | |
#pragma mark - observe | |
- (void)observeValueForKeyPath:(NSString *)keyPath | |
ofObject:(id)object | |
change:(NSDictionary *)change | |
context:(void *)context | |
{ | |
if (object == self.queueLiveQuery) { | |
for (CBLQueryRow* row in self.queueLiveQuery.rows) { | |
NSLog(@"found a row for demo queue, queue_id: %@, queue: %@", row.key, row.value); | |
self.QueueLabel.text = row.value; | |
} | |
} | |
} | |
#pragma mark - helper | |
-(NSString *) randomString { | |
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
NSMutableString *randomString = [NSMutableString stringWithCapacity: 10]; | |
for (int i=0; i<10; i++) { | |
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform((int)[letters length])]]; | |
} | |
return randomString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment