Storyline - mesosfer/Mesosfer-Cubeacon-iOS GitHub Wiki

Mesosfer provide a specialized bucket Storyline that automatically handles much of the functionality required for storyline management.

Objects and Types

Here is some predefined Storyline object :

  • title => NSString, define the title of a storyline.
  • isEnabled => Boolean, define the state of storyline, whether it is enabled or not.

Saving Storylines

Let’s say you want to save the MFStoryline described above to the Mesosfer Cloud with saveAsyncWithBlock: method:

// create new storyline object with title
MFStoryline *storyline = [MFStoryline storylineWithTitle:@"Entering-region-storyline"];
// set isEnabled
storyline.isEnabled = YES;
// execute save async using callback
[storyline saveAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    // check if there is an exception happen
    if (error) {
        NSLog(@"Error saving storyline, cause: %@", error);
        return;
    }
    
    NSLog(@"Storyline saved.");
}];

Retrieve Storylines

If you need to fetch a Storyline with the latest data that is in the cloud, you can call the fetchAsyncWithBlock: method like so:

// create storyline from existing objecId
MFStoryline *storyline = [MFStoryline storylineWithObjectId:@"storylineObjectId"];
[storyline fetchAsyncWithBlock:^(id  _Nullable object, NSError * _Nullable error) {
    // check if there is an exception happen
    if (error) {
        NSLog(@"Error fetching storyline, cause: %@", error);
        return;
    }
    
    NSLog(@"Storyline fetched.");
}];

This will automatically update storyline with the latest data from cloud.

Querying Storyline

There are many other ways to retrieve data with MFQuery. You can retrieve many data at once, put conditions on the data you wish to retrieve.

Basic Query

In many cases, there is a condition that need to specify which datas you want to retrieve. The MFQuery offers different ways to retrieve an array of MFStorylines. The general pattern is to create a MFQuery, put conditions on it, and then retrieve a NSArray of matching MFStorylines using the findAsyncWithBlock: method. For example, to retrieve MFStorylines data where isEnabled, use the whereEqualTo method to constrain the value for a key:

MFQuery *query = [MFStoryline query];
[query whereKey:STORYLINE_KEY_IS_ENABLED equalTo:@YES];
[query findAsyncWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
    if (error) {
        NSLog(@"Error finding storyline, cause: %@", error);
        return;
    }
    
    NSLog(@"Found storyline: %lu", objects.count);
}];

Query Constraint

Refer to this link to learn more about query constraint.

Update Storyline

After getting the MFStoryline data, you can update your data that stored in cloud using method saveAsyncWithBlock:.

MFStoryline *storyline; // fetched storyline data
// update Storyline data
storyline.title = @"Exiting-region-storyline";
storyline.isEnabled = NO;
// execute save data
[storyline saveAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    // check if there is an exception happen
    if (error) {
        NSLog(@"Error updating storyline, cause: %@", error);
        return;
    }
    
    NSLog(@"Storyline updated.");
}];

Delete Storyline

To delete a MFStoryline from the Mesosfer Cloud, use method deleteAsyncWithBlock: :

// the current Storyline object to delete
MFStoryline *storyline;
// execute delete storyline async
[storyline deleteAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    // check if there is an exception happen
    if (error) {
        NSLog(@"Error deleting storyline, cause: %@", error);
        return;
    }
    
    NSLog(@"Storyline deleted.");
}];