如何使用Unity监听DynamoDB上的更改?

时间:2019-11-18 19:03:46

标签: c# unity3d amazon-dynamodb

我正在开发一个VR应用程序,需要侦听DynamoDB表上的更改。我已经设法通过带有Unity的AWS开发工具包连接到表,但是现在控制器仅从表中提取一次数据。我该如何设置它,以便在表上的值更改时(类似于firebase的用法)触发一个方法?

这是我的代码示例:

public class DBListenerController: MonoBehaviour {
    private IAmazonDynamoDB client;
    private DynamoDBContext context;
    private AWSCredentials credentials;

    void Start() {
        UnityInitializer.AttachToGameObject(this.gameObject);
        AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;

        credentials = new CognitoAWSCredentials ("Identity pool ID", RegionEndpoint.USEast1);
        client = new AmazonDynamoDBClient(credentials, RegionEndpoint.USEast1);
        context = new DynamoDBContext(client);

        PerformReadOperation();
    }

    public void PerformReadOperation()
    {
        // Retrieve the table data 
        DataObject dataRetrieved = null;
        context.LoadAsync<DataObject>("HASH",(result)=>
            {
                if(result.Exception == null )
                {
                    dataRetrieved = result.Result as DataObject;
                    onValueChanged(dataRetrieved.value);
                }
            });

    }

    void onValueChanged(string changedValue) {
        Debug.Log("New Value: ", changedValue);
    }
}

0 个答案:

没有答案