Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 15, 2015 13:28
Show Gist options
  • Save ChrisRisner/5267043 to your computer and use it in GitHub Desktop.
Save ChrisRisner/5267043 to your computer and use it in GitHub Desktop.
iOS Mobile Services Storage
- (void) createContainer:(NSString *)containerName withPublicSetting:(BOOL)isPublic withCompletion:(CompletionBlock) completion {
NSDictionary *item = @{ @"containerName" : containerName };
NSDictionary *params = @{ @"isPublic" : [NSNumber numberWithBool:isPublic] };
[self.containersTable insert:item parameters:params completion:^(NSDictionary *result, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", result);
// Let the caller know that we finished
completion();
}];
}
- (void) createTable:(NSString *)tableName withCompletion:(CompletionBlock) completion {
NSDictionary *item = @{ @"tableName" : tableName };
[self.tablesTable insert:item completion:^(NSDictionary *result, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", result);
// Let the caller know that we finished
completion();
}];
}
- (void) deleteBlob:(NSString *)blobName fromContainer:(NSString *)containerName withCompletion:(CompletionBlock) completion {
NSDictionary *idItem = @{ @"id" :@1 };
NSDictionary *params = @{ @"containerName" : containerName, @"blobName" : blobName };
[self.blobsTable delete:idItem parameters:params completion:^(NSNumber *itemId, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", itemId);
// Let the caller know that we finished
completion();
}];
}
- (void) deleteContainer:(NSString *)containerName withCompletion:(CompletionBlock) completion {
NSDictionary *idItem = @{ @"id" :@1 };
NSDictionary *params = @{ @"containerName" : containerName };
[self.containersTable delete:idItem parameters:params completion:^(NSNumber *itemId, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", itemId);
// Let the caller know that we finished
completion();
}];
}
- (void) deleteTable:(NSString *)tableName withCompletion:(CompletionBlock) completion {
NSDictionary *idItem = @{ @"id" :@1 };
NSDictionary *params = @{ @"tableName" : tableName };
[self.tablesTable delete:idItem parameters:params completion:^(NSNumber *itemId, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", itemId);
// Let the caller know that we finished
completion();
}];
}
- (void) deleteTableRow:(NSDictionary *)item withTableName:(NSString *)tableName withCompletion:(CompletionBlock) completion {
//Have to send over all three as params since Mobile Services
//will strip everything but the ID from the item dictionary
NSDictionary *params = @{ @"tableName" : tableName ,
@"partitionKey" : [item objectForKey:@"PartitionKey"] ,
@"rowKey" : [item objectForKey:@"RowKey"]};
[self.tableRowsTable delete:item parameters:params completion:^(NSNumber *itemId, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", itemId);
// Let the caller know that we finished
completion();
}];
}
- (void) getSasUrlForNewBlob:(NSString *)blobName forContainer:(NSString *)containerName withCompletion:(CompletionWithSasBlock) completion {
NSDictionary *item = @{ };
NSDictionary *params = @{ @"containerName" : containerName, @"blobName" : blobName };
[self.blobsTable insert:item parameters:params completion:^(NSDictionary *item, NSError *error) {
NSLog(@"Item: %@", item);
completion([item objectForKey:@"sasUrl"]);
}];
}
-(StorageService *) init
{
// Initialize the Mobile Service client with your URL and key
MSClient *newClient = [MSClient clientWithApplicationURLString:@"https://mobileserviceurl.azure-mobile.net/"
withApplicationKey:@"applicationkey"];
// Add a Mobile Service filter to enable the busy indicator
self.client = [newClient clientwithFilter:self];
self.tablesTable = [_client getTable:@"Tables"];
self.tableRowsTable = [_client getTable:@"TableRows"];
self.containersTable = [_client getTable:@"BlobContainers"];
self.blobsTable = [_client getTable:@"BlobBlobs"];
self.tables = [[NSMutableArray alloc] init];
self.busyCount = 0;
return self;
}
- (void) insertTableRow:(NSDictionary *)item withTableName:(NSString *)tableName withCompletion:(CompletionBlock) completion {
NSDictionary *params = @{ @"table" : tableName };
[self.tableRowsTable insert:item parameters:params completion:^(NSDictionary *result, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", result);
// Let the caller know that we finished
completion();
}];
}
- (void) refreshBlobsOnSuccess:(NSString *)containerName withCompletion:(CompletionBlock) completion {
NSString *queryString = [NSString stringWithFormat:@"container=%@", containerName];
[self.blobsTable readWithQueryString:queryString completion:^(NSArray *results, NSInteger totalCount, NSError *error) {
[self logErrorIfNotNil:error];
self.blobs = [results mutableCopy];
// Let the caller know that we finished
completion();
}];
}
- (void) refreshContainersOnSuccess:(CompletionBlock) completion{
[self.containersTable readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error) {
[self logErrorIfNotNil:error];
self.containers = [results mutableCopy];
// Let the caller know that we finished
completion();
}];
}
- (void) refreshTableRowsOnSuccess:(NSString *)tableName withCompletion:(CompletionBlock) completion {
NSString *queryString = [NSString stringWithFormat:@"table=%@", tableName];
[self.tableRowsTable readWithQueryString:queryString completion:^(NSArray *results, NSInteger totalCount, NSError *error) {
[self logErrorIfNotNil:error];
self.tableRows = [results mutableCopy];
// Let the caller know that we finished
completion();
}];
}
- (void) refreshTablesOnSuccess:(CompletionBlock) completion{
[self.tablesTable readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error) {
[self logErrorIfNotNil:error];
self.tables = [results mutableCopy];
// Let the caller know that we finished
completion();
}];
}
- (void) updateTableRow:(NSDictionary *)item withTableName:(NSString *)tableName withCompletion:(CompletionBlock) completion {
NSDictionary *params = @{ @"table" : tableName };
[self.tableRowsTable update:item parameters:params completion:^(NSDictionary *result, NSError *error) {
[self logErrorIfNotNil:error];
NSLog(@"Results: %@", result);
// Let the caller know that we finished
completion();
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment