Skip to content

Instantly share code, notes, and snippets.

@msv
msv / synchronousDatastoreCalls.m
Created July 31, 2013 19:20
Synchronous Datastore calls
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[[[SMClient defaultClient] dataStore] createObject:anObject inSchema:@"schema" onSuccess:^(NSDictionary *object, NSString *schema){
// Do something with object
dispatch_group_leave(group);
} onFailure:^(NSError *error, NSDictionary* object, NSString *schema){
// Handle error
dispatch_group_leave(group);
@msv
msv / gist:5733065
Created June 7, 2013 23:14
MOBAppDelegate.m
#import "MOBAppDelegate.h"
#import "MOBLoginViewController.h"
#import "MOBTabBarController.h"
#import "MOBViewPhotoViewController.h"
#import "StackMobPush.h"
#import <FacebookSDK/FacebookSDK.h>
@implementation MOBAppDelegate
@synthesize client = _client;
// ... prepare objects for save
// ready for save
// create dispatch queue
dispatch_queue_t queue = dispatch_queue_create("queue", NULL);
// save object
[[SMClient defaultClient] coreDataStore] saveWithSuccessCallbackQueue:queue failureCallbackQueue:queue onSuccess:^{
// Success block called on queue (separate thread)
// In the method where you make the StackMob call
WhateverAppDelegate * = (WhateverAppDelegate *)[[UIApplication sharedApplication] delegate];
[[[self.appDelegate client] dataStore] performSomeTask:task onSuccess:successBlock onFailure:^(NSError *error) {
[self.appDelegate handleError:error];
}];
// Then, in your App Delegate file, create the handleError: method to handle all your StackMob errors without
// having to repeat code, which might look something like this:
-(void)uploadDataToServer{
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Apartment" inManagedObjectContext:self.managedObjectContext];
NSData *imageData = UIImageJPEGRepresentation(self.uploadImageView.image, 0.4);
NSString *picData = [SMBinaryDataConversion stringForBinaryData:imageData name:@"apartment.jpg" contentType:@"image/jpg"];
NSString* apartmentType = self.apartmentTypeControl.selectedSegmentIndex == 0 ? @"House" : @"Flat";
ApartmentCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ApartmentCell" forIndexPath:indexPath];
Apartment *apartment = [self.apartments objectAtIndex:indexPath.row];
NSNumber* roomCount = [apartment valueForKey:@"roomCount"];
NSString* roomCountText = [NSString stringWithFormat:@"%d Bed", [roomCount intValue]];
NSNumber* price = [apartment valueForKey:@"price"];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
-(void)getAllApartments
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading...";
NSFetchRequest *fetch = [[NSFetchRequest alloc] initWithEntityName:@"Apartment"];
[self.managedObjectContext executeFetchRequest:fetch onSuccess:^(NSArray *results) {
self.apartments = results;
-(IBAction)logInPressed:(id)sender
{
[self.client loginWithUsername:self.userTextField.text password:self.passwordTextField.text onSuccess:^(NSDictionary *results) {
if ([[[self appDelegate] client] isLoggedIn]) {
NSLog(@"Logged in");
}
[self performSegueWithIdentifier:@"list" sender:self];
-(IBAction)signUpUserPressed:(id)sender
{
User *newUser = [[User alloc] initIntoManagedObjectContext:self.managedObjectContext];
[newUser setValue:self.userRegisterTextField.text forKey:[newUser primaryKeyField]];
[newUser setPassword:self.passwordRegisterTextField.text];
[self.managedObjectContext saveOnSuccess:^{
[self.navigationController popViewControllerAnimated:YES];
} onFailure:^(NSError *error) {
#import "AppDelegate.h"
#import "ADVTheme.h"
#import "StackMob.h"
#define PUBLIC_KEY @"YOUR_PUBLIC_KEY"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{