Skip to content

Instantly share code, notes, and snippets.

@EchoAbstract
Created February 28, 2012 15:11
Show Gist options
  • Save EchoAbstract/1933013 to your computer and use it in GitHub Desktop.
Save EchoAbstract/1933013 to your computer and use it in GitHub Desktop.
Story with Comment
// Defined somewhere in your headers
// Class for representing Stories
@interface Story : NSObject <KCSPersistable>
NSString *storyId; // In your mapping function this should map to "_id"
NSString *name;
NSString *reporter;
@end
// Class for representing comments
@interface Comment : NSObject <KCSPersistable>
NSString *commentId; // In your mapping function this should map to "_id"
NSString *storyId;
NSString *username;
NSString *text;
@end
// In your Submit comment view controller
// Grab a handle for the comments backend collection
KCSCollection *comments = [[[KCSClient sharedClient]
collectionFromString:@"comments"
withClass:[Comment class]] retain];
// Build a new comment
Comment *c = [[Comment alloc] init];
c.storyId = self.currentStory.storyId;
c.username = self.userNameTextField.text;
c.text = self.commentTextArea.text;
// Save the comment to the backend
[c saveToCollection:comments WithDelegate:self];
// In your comment view controller
// This function is called when you want to pull new comments from the backend
- (void)fetchComments:
{
Grab a handle to the comments collection
KCSCollection *comments = [[[KCSClient sharedClient]
collectionFromString:@"comments"
withClass:[Comment class]] retain];
// Build a filter to match all relevant comments
KCSQuery *q = [KCSQuery queryOnField:@"storyId" withExactMatchForValue:self.currentStory.storyId];
comments.query = q;
// Pull down all comments whose storyId == self.currentStory.storyId
[comments fetchWithDelegate: self];
}
// Later...
// Called when results are ready
- (void)collection:(KCSCollection *)collection didCompleteWithResult:(NSArray *)result
{
// Update the displayed comments with the newly fetched comments
self.commentsArray = result;
// ... Any other actions necessary
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment