Episode #30

Parse

17 minutes
Published on August 23, 2012

This video is only available to subscribers. Get access to this video and 572 others.

In this episode I build an app with Parse, a service that provides custom data storage, files, push notifications, a geolocation support.

Episode Links

Saving a new object in Parse

PFObject *object = [[PFObject alloc] initWithClassName:@"Car"];
[object setObject:@"Acura" forKey:@"Make"];
[object setObject:@"TL" forKey:@"Model"];
[object save];

This will create a new object in a "Cars" collection on the Parse dashboard. You can add whatever properties you want, they will be created dynamically for you on the server.

Showing Parse records in a table

The easiest way to do this is to inherit from PFQueryTableViewController.

@interface FGPhotosViewController : PFQueryTableViewController
@end

@implementation FGPhotosViewController

- (id)init {
  self = [super initWithStyle:UITableViewStylePlain className:@"Cars"];
  if (self) {
    self.objectsPerPage = 30;  //enable automatic paging
    self.pullToRefreshEnabled = YES;
  }
}

- (void)viewDidLoad {
  [super viewDidLoad]
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:identifier];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor colorWithRed:0.7 green:0.5 blue:0.3 alpha:1.0];
    }

    cell.textLabel.text = [object objectForKey:@"make"];
    cell.imageView.file = [object objectForKey:@"model"];

    return cell;
}

@end

Uploading an image

Images are uploaded using the PFFile class. All you need is an NSData and you can easily save it to Parse:

    NSData *imageData = UIImagePNGRepresentation(carImage);
    self.imageFile = [PFFile fileWithData:imageData];
    [self.imageFile saveInBackground];

Then you can associate this file with a Parse record just like setting any other property:

PFObject *car = [[PFObject alloc] initWithClassName:@"Car"];
[car setObject:self.imageFile forKey:@"photo"];
[car saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
  if (success) { ... 
  } else {
    NSLog(@"ERROR: %@", error);
  }
}];