Amazon.CognitoSync.SyncManager.Dataset.Put C# (CSharp) Method

Put() public method

Puts a Amazon.CognitoSync.SyncManager.Record with the given key and value into the Dataset. If a Amazon.CognitoSync.SyncManager.Record with the same key exists, its value will be overwritten. If a Amazon.CognitoSync.SyncManager.Record is marked as deleted previously, then it will be resurrected with new value while the sync count continues with previous value. No matter whether the value changes or not, the record is considered as updated, and it will be written to Cognito Sync service on next synchronize operation. If value is null, a ArgumentNullException will be thrown.
public Put ( string key, string value ) : void
key string Key of the record
value string String value of a to be put into the ///
return void
        public void Put(string key, string value)
        {
            Local.PutValue(IdentityId, DatasetName,
                           DatasetUtils.ValidateRecordKey(key), value);
        }

Usage Example

    private bool HandleDatasetMerged(Dataset localDataset, List<string> mergedDatasetNames)
    {
		Debug.LogWarning("Sync merge");

        if (mergeInCourse) {
			Debug.LogWarning ("Already in a merge");
			return false;            
        }

		//This variable can be used to hold the game from actually starting
        //and show a loading indicator to the user meanwhile
        mergeInCourse = true;
        
        //Delete the merged datasets and just keep the local data
        foreach (string name in mergedDatasetNames) {

			Dataset mergedDataset = syncManager.OpenOrCreateDataset(name);
			//mergedDataset.Delete(); //Remove any data we could have from a previous execution of this handler
            //Lambda function to delete the dataset after fetching it
            EventHandler<SyncSuccessEvent> lambda =null;
            Debug.Log ("fetching dataset to merge: " + name);
            lambda = (object sender, SyncSuccessEvent e) => { 
                //Actual merge code: We join the local characters and the remote characters into a new single dataset.
                List<string> allCharacters = new List<string>();
				ICollection<string> existingCharacters = localDataset.GetAll().Values;
				ICollection<string> newCharacters = mergedDataset.GetAll().Values;
				Debug.LogWarning(localDataset.Metadata.DatasetName + ": " + existingCharacters.Count);
				Debug.LogWarning(mergedDataset.Metadata.DatasetName + ": " + newCharacters.Count);
                allCharacters.AddRange(existingCharacters);
                allCharacters.AddRange(newCharacters);
                int i = 0;
                foreach (string characterString in allCharacters) {
					localDataset.Put (indexToKey(i++), characterString);
                }

                Debug.Log ("deleting merged dataset: " + name);
				mergedDataset.Delete(); //Delete the dataset locally
				mergedDataset.OnSyncSuccess -= lambda; //We don't want this callback to be fired again
				mergedDataset.OnSyncSuccess += (object s2, SyncSuccessEvent e2) => {
                    Debug.Log ("merge comleted (dataset merged and deleted)");
                    mergeInCourse = false;
					localDataset.Synchronize(); //Continue the sync operation that was interrupted by the merge
                };
				mergedDataset.Synchronize(); //Synchronize it as deleted, failing to do so will leave us in an inconsistent state

            };
            
			mergedDataset.OnSyncSuccess += lambda;
			mergedDataset.Synchronize(); //Asnchronously fetch the dataset
        }

        // returning true allows the Synchronize to continue and false cancels it
		return true;
    }