Oracle Mobile Cloud Service

Storage

diagram of Storage within MCS architecture

The Storage API enables you to create and work with collections of data in your mobile apps, including data uploaded by users.

described in surrounding text

Let's create a collection and then configure the sample app to add some objects to that collection.

Note: Before you start, complete the Get Started with Mobile Development tutorial that is offered within Oracle Mobile Cloud Service on its Home page. That tutorial provides the mobile backend and the quickstart project that you will use here.

part 1Create a collection

  1. Click menu icon in the upper left corner of the page and select Applications > Storage.
  2. Click New Collection.
  3. Create a collection called Incident_Reports.
  4. In the New Collection dialog, select Shared.
  5. Click Create.

part 2Set access role

You can set access roles based on Mobile User Management roles that have been defined for the realm that mobile backend is using.

  • In the Properties page of the collection, type Technician in the Read-Write Access field and click Save.

part 3Associate a mobile backend

So that your collection can be accessed by an app, you need to associate it with a mobile backend corresponding to the app.

  1. In the breadcrumbs at the top of the page, click Storage to display that lists all collections.
  2. Make sure that Incident_Reports is selected, click More, and select Associate Mobile Backends.
  3. Type the name of the mobile backend you created in the Get Started with Mobile Development tutorial and click Add.

part 4Add object from the mobile app

The next step is to open the sample project you downloaded as part of the Get Started with Mobile Development tutorial. This project contains code for uploading an object to a collection. You just need to fill in the details in the client code. To keep it simple, you will merely upload some text that you specify.

Here are the instructions for the Android and iOS versions of the sample project.

Android

Open the file GettingStartedAndroid/app/src/main/java/example/com/gettingstartedandroid/UploadText_Activity.java and scroll down to the following text block:

    private String collectionID = "YOUR_COLLECTION_ID";
    private String payload = "YOUR_PAYLOAD";
    private String contentType = "YOUR_CONTENT_TYPE";

For each variable, fill in the appropriate values:

  • For collection_Id, fill in Incident_Reports.
  • For payload, fill in Empty incident report
  • For contentType, fill in text/plain.

Open the AndroidManifest.xml file, search for oracle.cloud.mobile.gettingstarted.UploadActivity and change it to oracle.cloud.mobile.gettingstarted.UploadText_Activity.

iOS

In the file GettingStartediOS/GettingStartediOS/UploadViewController.m, scroll down to the following text block:

NSString* collection_Id = @"";
NSString* payload = @"";
NSString* contentType = @"";

For each variable, fill in the appropriate values:

  • For collection_Id, fill in Incident_Reports.
  • For payload, fill in Empty incident report.
  • For contentType, fill in text/plain.

Windows

In the file GettingStartedWindows81/MainPage.xaml.cs, search for the string "YOUR_STORAGE_COLLECTION_NAME" and replace it with Incident_Reports:

In the same file, scroll down to the following text block:

private async void uploadButton_Click(object sender, RoutedEventArgs e)
{
	var backend = ((App)App.Current).Backend;

	var storage = backend.GetService();
	var collection = await storage.GetCollectionAsync(MainPage.CollectionName);

	// Upload an object
	var storageObject = new StorageObject(collection);
	storageObject.Name = "Hello.txt";
	storageObject.LoadPayload("Hello Windows from Mobile Cloud Service", "plain/text");
	storageObject = await collection.PostObjectAsync(storageObject);
	string objectId = storageObject.Id;

	await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => {
		MessageDialog dialog = new MessageDialog("Upload completed successfully", "Status");
		await dialog.ShowAsync();
	});
}

For storageObject.LoadPayload, replace Hello Windows from Mobile Cloud Service with the the text of your choice.

Cordova

Open the file www/app/shared/backend/backendService.js and scroll down to the following text block::

var collectionId = 'YOUR_STORAGE_COLLECTION_NAME';
var fileName = 'YOUR_FILE_NAME';
var payload = 'YOUR_PAYLOAD';
var contentType = 'YOUR_PAYLOAD_CONTENT_TYPE';

For each variable, fill in the appropriate values:

  • For fileName, fill in Incident_Reports.txt.
  • For collectionId, fill in Incident_Reports.
  • For payload, fill in Empty incident report.
  • For contentType, fill in text/plain.

part 5Run the app

  1. In your IDE, run the application.
  2. In the running app, click Sign In, and enter a user name and password.
  3. Click Upload Incident.

part 6Verify update of the collection in the mobile backend

In MCS, navigate to the collection to check its contents:

  1. In the menu on the left, click Mobile Backends. (If you have hidden the menu, click menu icon in the upper left corner of the page and select Applications > Mobile Backends.)
  2. Select your mobile backend and click Open.
  3. In the left navigation bar, select Storage.
  4. Select the Incident_Reports collection and click Open.
  5. Click the Content tab and note that the object has been added.

part 7Next steps