First, what is the AppGallery Connect cloud database service?

First look at the official introduction: developer.huawei.com/consumer/cn…

CloudDB of Huawei AppGallery Connect is a database product for end-to-end cloud collaboration, providing end-to-end cloud data collaborative management, unified data model, and rich data management apis. In addition to ensuring the availability, reliability, consistency, and security of data, it can achieve seamless data synchronization between the client and the cloud and provide offline support for applications, helping developers quickly build end-to-end cloud and multi-terminal collaborative applications.

Is it a little hard to understand? Let me translate it in plain English:

CloudDB is for our developers. It provides an end-to-end cloud collaborative database service, which can be easily integrated into APP applications through SDK and API interfaces, making it safe and reliable to use. When integrating, developers do not need to pay attention to the server construction and deployment operation and maintenance, and directly use the SDK and API interface.

After translation, is it very easy to understand ha ha.

Let’s take a look at how to quickly use CloudDB in Android apps. After my trial, found that the use is actually very convenient, summed up, is the following three steps:

1. Interface operations: Create object types and storage areas

2. Application prerequisites: Export object types and perform user authentication.

3. Integrate CloudDB in Android project: integrate SDK, use interface to add, delete, change and check:

1. Interface operation:

Written in the most front: CloudDB is still in the Beta stage, email need to apply for opening the service, you can refer to this link: developer.huawei.com/consumer/cn…

Interface operations mainly include creating object types and creating storage areas

1.1. What is the object type?

Simply put, an object type is a table in your database that you store data in, and each object type is a table. For a simple example, if you have an Excel file for storing data, each Excel can create a different worksheet, and each worksheet is like an object type in CloudDB. For example, I am a teacher, and the information stored in worksheet 1 is the student information, including the gender and student number of the student. The results of students stored in Table 2 include the student id number and the scores of each subject.

So that makes sense.

1.2. What is the storage area?

An area used to store data. The different storage areas are isolated from each other. To continue the example above: I, as a teacher, may lead multiple classes, and when recording students’ grades, different classes use different Excel files. Each Excel file includes a student information sheet and a transcript.

In your example above, each Excel file is a storage area, and the two Excel files are unrelated to each other. Each Excel has a different worksheet, and each worksheet is an object type.

1.3. Create an object type

With the concept behind you, here’s how to do it: If you’ve already signed up for CloudDB and have access to it.

1. Choose My Project > Build > Cloud Database. On the Object Type TAB page, click Add to create an object type.

StudentInfo = primary key (****Upsert); delete (****Upsert);

1.4. Create a storage area

This is a lot easier, just go to the Storage TAB, click Add datastore name, click OK.

Procedure before Application

To properly use CloudDB in Android applications, there are two pre-steps: exporting object types and user authentication.

1. Export object type:

Export the object type to your local Android project, so that the Android project can synchronize with your cloud database.

After the export. Remember to put it in your Android project. For example, I put the exported object types in the Model path

Integrate CloudDB in Android project

1. Integrate SDK

1. Download JSON: Go to “My Project -> Project Settings -> Apps”, download the JSON file, and place it in the app path of your Android project:

2. Configure the project-level build.gradle file

buildscript { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } dependencies { The classpath 'com. Android. Tools. Build: gradle: 4.0.1' classpath 'com. Huawei. Agconnect: agcp: 1.4.2.301' allprojects {}} repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} }} task clean(type: Delete) { delete rootProject.buildDir}Copy the code

3. Configure the application level build.gradle file

apply plugin: 'com.android.application'apply plugin: 'com.huawei.agconnect' ... Android {... CompileOptions {sourceCompatibility = 1.8 targetCompatibility = 1.8}}dependencies {... Implementation 'com. Huawei. Agconnect: agconnect - auth: 1.4.2.300' / / huawei auth service, For user authentication implementation 'com. Huawei. Agconnect: agconnect - database: 1.2.3.301' / / CloudDB SDK}Copy the code

1.1 Front Operations

CloudDB also needs to integrate huawei authentication services. The Auth SDK integrated in the previous step is for this purpose.

1. First go to the AGC interface to open the authentication service. Here, for the simplest integration, I will only use anonymous authentication to give you an example: choose “My Project -> Build -> Authentication Service” to start anonymous authentication.

2. Code related to anonymous authentication:

AGConnectAuth.getInstance().signInAnonymously().addOnSuccessListener(new OnSuccessListener<SignInResult>() {     @Override     public void onSuccess(SignInResult signInResult) {         // onSuccess         AGConnectUser user = signInResult.getUser();     } }).addOnFailureListener(new OnFailureListener() {     @Override     public void onFailure(Exception e) {         // onFail     } });
Copy the code

2. Initialize

Initialization consists of three parts: initializing AGConnectCloudDB, creating the object type, and creating and opening the CloudDB Zone.

1. Define the parameters to use on the outer layer of onCreate:

private AGConnectCloudDB mCloudDB; private CloudDBZoneConfig mConfig; private CloudDBZone mCloudDBZone;
Copy the code

2. Initialize AGConnectCloudDB.

AGConnectCloudDB.initialize(this);
Copy the code

3. Get AGConnectCloudDB instance and create the object type

mCloudDB = AGConnectCloudDB.getInstance(); try {     mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo()); } catch (AGConnectCloudDBException e) {     Log.e("CloudDB", "createObjectType Failed " + e.getMessage()); }
Copy the code

Create and open the CloudDB Zone

Config = new CloudDBZoneConfig("classs1",         CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,         CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC); mConfig.setPersistenceEnabled(true); try {     CloudDBZone = mCloudDB.openCloudDBZone(mConfig, true); } catch (AGConnectCloudDBException e) {     Log.e("CloudDB", "openCloudDBZone failed: " + e.getMessage()); }
Copy the code

Note that you can also use the asynchronous openCloudDBZone2 method, which is not described here. See the following documents for details.

Developer.huawei.com/consumer/cn…

3. Database operation:

Now you can have fun with the database. Let me take a query as an example:

1. Insert test1 and test2 into AGC interface for testing:

2. Then go back to the Android project and run executeQuery to query the full number.

Task<CloudDBZoneSnapshot<StudentInfo>> queryTask = mCloudDBZone.executeQuery(         CloudDBZoneQuery.where(StudentInfo.class),         CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY); queryTask.addOnSuccessListener(new OnSuccessListener<CloudDBZoneSnapshot<StudentInfo>>() {     @Override     public void onSuccess(CloudDBZoneSnapshot<StudentInfo> snapshot) {         //         CloudDBZoneObjectList<StudentInfo> InfoCursor = snapshot.getSnapshotObjects();         ArrayList<StudentInfo> infoList = new ArrayList<>();         StudentInfo studentInfo = new StudentInfo();         try {             while (InfoCursor.hasNext()) {                 studentInfo = InfoCursor.next();                 infoList.add(studentInfo);             }             Log.i("CloudDB", "query success: " + JSONArray.toJSONString(studentInfo));         } catch (AGConnectCloudDBException e) {             Log.e("CloudDB", "query failed: " + e.getMessage());         }         snapshot.release();     } });
Copy the code

Select * from Logcat; select * from Logcat;

4, the subsequent add, delete and change operations, I will not elaborate, you can directly see the document or API reference

Configuration guide: developer.huawei.com/consumer/cn…

API reference: developer.huawei.com/consumer/cn…

conclusion

Although the documentation looks a lot, in practice, there are only three steps.

1. Create object types and storage areas on the cloud side:

2. Export object types to the Android project

3. Integrate SDK and use API interface in Android project.

In three steps, a database system is integrated without any database setup or deployment, and CloudDB is still free to use.

Finally, attach the official CloudDB document. If you need it, you can check it by yourself:

Configuration guide: developer.huawei.com/consumer/cn…

API reference: developer.huawei.com/consumer/cn…

Demo reference: github.com/AppGalleryC…

The original link: developer.huawei.com/consumer/cn…

Original author: Mayism