Developer Reference

Developing for iOS platform

Drag and drop the folder you just downloaded from CloudyRec into your Xcode project. You might want to copy all the files into your project folder as well.

2. Using resource methods

CloudyRec generates each resource we defined as an individual class with OOP designs in mind. That means if we define 2 resources as user and blog_post, we’ll get 2 classes with their respective names. In the following code examples we’ll use the ‘user’ resource we created previously as an example.

For demo purpose, we’ll omit the steps like exception handling, importing header files and packages. You are also encouraged to explore the resource classes being generated.

2.1. Creating data

Creating a new record can be done in multiple ways. The simplest way is to pass all the required attribute values to the constructor when creating a new resource object. Then call the saveWithCompletion method to upload the data to CloudyRec.


User* newUser = [[User alloc] initWithUser:@"user id" Password:@"secret pwd"];
[newUser saveWithCompletion:^(BOOL respond) {
    NSLog(@"Signup successfull.");
} onFailure:^(NSError *respond) {
    NSLog(@"Error sign up %@", respond.description);
}];
[newUser release];

2.2. Authenticating an existing user

If a resource is being checked as “is authenticable” during resource creation, we can use the generated authenticate method to login with a user id and password.


User *user = [[User alloc] init];
[user authenticate:@"user id" Passwd:@"pwd" onCompletion:^(BOOL respond) {
    if (respond) {
        NSLog(@"Auth successed.");
    }
    else {            
        NSLog(@"Auth failed.");
    }
} onFailure:^(NSError *respond) {
    NSLog(@"Auth error: %@", respond.description);
}];
[user release];

2.3. Retrieving data

To retrieve the records from CloudyRec servers, you can use the listWithCompletion method. This method accepts a parameter to specify selection filters. If you pass nil, it’ll return all the records from that resource.

Selecting all users:


User *user = [[User alloc] init];
[user list:nil onComplete:^(NSArray *respond) {
    NSLog(@"Total users: %d", [respond count]);
} onFailure:^(NSError *respond) {
    NSLog(@"List error: %@", respond.description);
}];
[user release];

Iterating through the result:


for (int i = 0; i < [respond count]; i++) {
    User *currentUser = (User *)[respond objectAtIndex:i];
    NSLog(@"User id: %@", currentUser.uname);
}

Selecting a particular user:


User *user = [[User alloc] init];
[user list:@"_uname=id_or_email_to_check_against" onComplete:^(NSArray *respond) {
    NSLog(@"Found %d record(s).", [respond count]);
} onFailure:^(NSError *respond) {
    NSLog(@"List error: %@", respond.description);
}];
[user release];

Other operators available:


>  (greater than)
<  (less than)
>= (greater than or equal to)
<= (less than or equal to)
!= (not equal to)

Unfortunately, logical operations such as ‘and’ and ‘or’ are not available yet.

2.4. Loading a particular resource

If you know the ID of a particular record you can load the details of that resource by setting its ID and calling the load method.


User *user = [[User alloc] init];
[user load:@"4f83fd59c0af7639d4000006" onComplete:^(BOOL respond) {
    NSLog(@"Loaded user: %@", user.uname);
} onFailure:^(NSError *respond) {
    NSLog(@"Loading failed: %@", respond.description);
}];
[user release];

2.5. Updating data

In order to edit and update a record, we first need to get the record ID that we want to edit. Then update its properties through getter/setter methods and call the saveWithCompletion method again.

Updating a particular user:


[user list:@"_uname=old_email@cloudyrec.com" onComplete:^(NSArray *respond) {
    User *currentUser = (User *)[respond objectAtIndex:0];
    NSLog(@"Current email: %@", currentUser.uname);
    currentUser.uname = @"new_email@cloudyrec.com";
    [currentUser saveWithCompletion:^(BOOL respond) {
        NSLog(@"Record successfully updated");
    } onFailure:^(NSError *respond) {
        NSLog(@"Update failed: %@", respond.description);
    }];
} onFailure:^(NSError *respond) {
    NSLog(@"Loading failed: %@", respond.description);
}];

2.6. Deleting data

Deleting a record is similar to updating a record. We first get the record ID we want to delete and the call the deleteWithCompletion method. Let’s delete all the records from our user resource.

Deleting a particular user:


[user list:@"_uname=old_email@cloudyrec.com" onComplete:^(NSArray *respond) {
    User *currentUser = (User *)[respond objectAtIndex:0];    
	[currentUser deleteWithCompletion:^(BOOL respond) {
		NSLog(@"Successfully deleted.");
	} onFailure:^(NSError *respond) {
		NSLog(@"Error: %@", respond.description);
	}];
} onFailure:^(NSError *respond) {
    NSLog(@"Loading failed: %@", respond.description);
}];