SimpleSample-customObjects-ios
Contents |
Sources
Project homepage on GIT — https://github.com/QuickBlox/quickblox-ios-sdk/tree/master/sample-custom-objects
Download ZIP - https://github.com/QuickBlox/quickblox-ios-sdk/archive/master.zip
Overview
This sample demonstrates how to work with Custom Objects QuickBlox API.
It allows to create any server side data structure, use it as you want, create any logic and a lot of others custom features.
It shows how to:
- Create own server data structure & use it. In our example we created data structure, that represents simple Note.
- Get, Create, Update, Delete your data using a lot of filters.
Guide: Get Started with Custom Objects API
Get QuickBlox account
http://admin.quickblox.com/register
Create application on Admin panel
http://admin.quickblox.com/apps/new
Also you can look through 5 min guide.
Connect QuickBlox to your application
To get the information on how to connect to the QuickBlox.framework, please, refer to the IOS-how-to-connect-Quickblox-framework page.
Add Custom Data structure to your application
Note: This guide based on Custom Objects REST API documentation. It is very helpful, describes full list of QuickBlox Custom Objects API features and need to be read during reading this guide.
To start using Custom Objects module you should create your data schema. Go to admin.quickblox.com, Custom Objects module page and press Add new class button. 'Add new class' popup will be appeared.
Enter Class name, add fields any you want. Allow 5 types of fields:
- Integer
- Float
- Boolean (true/false)
- String
- Array (of Integers, Floats, Booleans, Strings)
Press Create class button - new class will be created
There are some predefined fields, that described in Custom Objects REST API Module description documentation.
Create record
There are 2 ways to create record:
- through Admin panel
- using iOS SDK
Create record through Admin panel
Just go to admin.quickblox.com, Custom Objects module page and press Add record button. 'Add new record' popup will be appeared. Fill any fields you want and press Add record button. New record will be added & shown in table.
Create record using iOS SDK
In order to create records you must be logged in and to act on user behalf - please refer to iOS Users API documentation.
To create record just use code bellow:
QBCOCustomObject *object = [QBCOCustomObject customObject]; object.className = @"Movie"; // your Class name // Object fields [object.fields setObject:@"Star Wars" forKey:@"name"]; [object.fields setObject:[NSNumber numberWithFloat:9.1f] forKey:@"rating"]; [object.fields setObject:[NSNumber numberWithBool:NO] forKey:@"documentary"]; [object.fields setObject:@"fantasy" forKey:@"genre"]; [object.fields setObject:@"Star Wars is an American epic space opera franchise consisting of a film series created by George Lucas." forKey:@"descriptions"]; [QBCustomObjects createObject:object delegate:self]; #pragma mark - #pragma mark QBActionStatusDelegate - (void)completedWithResult:(Result *)result{ // Create record result if(result.success && [result isKindOfClass:QBCOCustomObjectResult.class]){ QBCOCustomObjectResult *createObjectResult = (QBCOCustomObjectResult *)result; NSLog(@"Created object: %@", createObjectResult.object); }else{ NSLog(@"errors=%@", result.errors); } }
Retrieve records
There are lots of operators that you can use to retrieve records:
- Retrieve records using search operators by your class fields
- Sort operators
- 'Pagination' operators
- Aggregation operators
Please refer to Custom Objects REST API Retrieve records documentation. All these operators & parameters you can use as well in your code.
For example, we want to retrieve 5 movies that:
- Have rating > 5.5
- Not documentary
- Movies must be sorted by rating field in ascending order
NSMutableDictionary *getRequest = [NSMutableDictionary dictionary]; [getRequest setObject:@"5.5" forKey:@"rating[gt]"]; [getRequest setObject:@"5" forKey:@"limit"]; [getRequest setObject:[NSNumber numberWithBool:NO] forKey:@"documentary"]; [getRequest setObject:@"rating" forKey:@"sort_asc"]; [QBCustomObjects objectsWithClassName:@"Movie" extendedRequest:getRequest delegate:self]; #pragma mark - #pragma mark QBActionStatusDelegate - (void)completedWithResult:(Result *)result{ // Get objects result if(result.success && [result isKindOfClass:QBCOCustomObjectPagedResult.class]){ QBCOCustomObjectPagedResult *getObjectsResult = (QBCOCustomObjectPagedResult *)result; NSLog(@"Objects: %@, count: %d", getObjectsResult.objects, getObjectsResult.count); }else{ NSLog(@"errors=%@", result.errors); } }
Update record
To update existing record you should know record ID. Let's update Movie with ID 502f7c4036c9ae2163000002 - set rating to 7.88:
QBCOCustomObject *object = [QBCOCustomObject customObject]; object.className = @"Movie"; [object.fields setObject:@"7.88" forKey:@"rating"]; object.ID = @"502f7c4036c9ae2163000002"; [QBCustomObjects updateObject:object delegate:self]; #pragma mark - #pragma mark QBActionStatusDelegate - (void)completedWithResult:(Result *)result{ // Update object result if(result.success && [result isKindOfClass:QBCOCustomObjectResult.class]){ QBCOCustomObjectResult *updateObjectResult = (QBCOCustomObjectResult *)result; NSLog(@"Object: %@", updateObjectResult.object); }else{ NSLog(@"errors=%@", result.errors); } }
Also there are Special update operators, that are very helpful for some purpose, e.g. update Array field etc.
Delete record
To delete existing record you should know record ID. Let's delete Movie with ID 502f83ed36c9aefa62000002:
NSString *ID = @"502f83ed36c9aefa62000002"; NSString *className = @"Movie"; [QBCustomObjects deleteObjectWithID:ID className:className delegate:self]; #pragma mark - #pragma mark QBActionStatusDelegate - (void)completedWithResult:(Result *)result{ // Delete object result if(result.success && [result isKindOfClass:QBCOCustomObjectResult.class]){ NSLog(@"Object was deleted successfully"); }else{ NSLog(@"errors=%@", result.errors); } }
Relations
We allow to organize relation between 2 objects - just use special field _parent_id.
For example, if you want to add Comments to movie - you can use code snippet bellow:
QBCOCustomObject *object = [QBCOCustomObject customObject]; object.className = @"Comment"; // your Class name // Object fields [object.fields setObject:@"The first film in the series was originally released on May 25, 1977, under the title Star Wars, by 20th Century Fox" forKey:@"text"]; [object.fields setObject:@"50aa4d8fefa357fa14000001" forKey:@"_parent_id"]; // Movie ID [QBCustomObjects createObject:object delegate:self]; #pragma mark - #pragma mark QBActionStatusDelegate - (void)completedWithResult:(Result *)result{ // Create record result if(result.success && [result isKindOfClass:QBCOCustomObjectResult.class]){ QBCOCustomObjectResult *createObjectResult = (QBCOCustomObjectResult *)result; NSLog(@"Created object: %@", createObjectResult.object); }else{ NSLog(@"errors=%@", result.errors); } }
This is strong reference - it means that if you will delete parent record (Movie) - all children records (Comments) will be deleted too.
This field (_parent_id) may be included in Retrieve/Create/Update queries as well.
Comments
Feel free to comment on this page using the form below.
blog comments powered by Disqus


