Search code examples
javaandroidillegalstateexceptionhuawei-developers

Failed to open the CloudDBZone because the object type has not been created


I'm trying to open an (existing) AGConnectCloudDB CloudDBZone with Android Java:

public void openCloudDBZone(String regionName, boolean isAllowToCreate) {
    CloudDBZoneConfig config = new CloudDBZoneConfig(regionName, syncProperty, accessProperty);
    this.db = AGConnectCloudDB.getInstance(AGConnectInstance.getInstance(), AGConnectAuth.getInstance());
    this.db.openCloudDBZone2(config, isAllowToCreate)
            .addOnSuccessListener(cloudDBZone -> {
                zone = cloudDBZone;
            }).addOnFailureListener(e -> {
                Log.e(LOG_TAG, e.getMessage());
            });
}

This fails with an IllegalStateException from com.huawei.hmf.tasks:

Failed to open the CloudDBZone because the object type has not been created.

I've defined the object-types in the AGConnect console and also as CloudDBZoneObject.
Version number is 1.5.4.300, which has ObjectTypeInfo but no ObjectTypeInfoHelper:

implementation 'com.huawei.agconnect:agconnect-cloud-database:1.5.4.300'

What does this error message mean and what to do about it?


Solution

  • After having found the "Export" button (the screenshot in the documentation doesn't match the GUI, where it can be found inside the menu), I've also found the ObjectTypeInfoHelper.java in the downloaded zip file. Working code for reference:

    private AGConnectCloudDB db;
    private CloudDBZone zone;
    
    private final CloudDBZoneConfig.CloudDBZoneSyncProperty syncProperty =
            CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE;
    
    private final CloudDBZoneConfig.CloudDBZoneAccessProperty accessProperty =
            CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC;
    
    private final CloudDBZoneQuery.CloudDBZoneQueryPolicy queryPolicy =
            CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_DEFAULT;
    
    /** class ObjectTypeInfoHelper originates from the downloaded zip file. */
    public void openCloudDBZone(String zoneName, boolean isAllowToCreate) {
        try {
            CloudDBZoneConfig config = new CloudDBZoneConfig(zoneName, syncProperty, accessProperty);
            this.db = AGConnectCloudDB.getInstance(AGConnectInstance.getInstance(), AGConnectAuth.getInstance());
            this.db.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());
            this.db.openCloudDBZone2(config, isAllowToCreate)
                    .addOnSuccessListener(cloudDBZone -> {
                        Log.d(LOG_TAG, "CloudDBZone opened: " + zoneName);
                        zone = cloudDBZone;
                    }).addOnFailureListener(e -> {
                        Log.e(LOG_TAG, e.getMessage());
                    });
        } catch (AGConnectCloudDBException e) {
            Log.e(LOG_TAG, e.getMessage());
        }
    }
    
    public void closeCloudDBZone() {
        try {
            this.db.closeCloudDBZone(this.zone);
            Log.d(LOG_TAG, "CloudDBZone closed.");
        } catch (AGConnectCloudDBException e) {
            Log.w(LOG_TAG, e.getMessage());
        }
    }
    

    The difference is ObjectTypeInfoHelper.getObjectTypeInfo().


    And I've also wrote a TypeConverter, which converts CloudDBZoneObject to BaseObservable (read) and BaseObservable to CloudDBZoneObject (write). Alike this data-binding isn't an issue anymore.