Skip to content

Instantly share code, notes, and snippets.

@justin
Created May 17, 2014 20:46
Show Gist options
  • Save justin/5983c0ef8f5fa5bf44e2 to your computer and use it in GitHub Desktop.
Save justin/5983c0ef8f5fa5bf44e2 to your computer and use it in GitHub Desktop.
public async void RemoveDevices()
{
CloudTableClient client = account.CreateCloudTableClient();
CloudTable devicesTable = client.GetTableReference("PushNotificationDeviceTable");
//// Good Form
await devicesTable.CreateIfNotExistsAsync();
// Entity Resolver
EntityResolver<PushNotificationDevice> deviceResolver = (pk, rk, ts, props, etag) =>
{
PushNotificationDevice resolvedEntity = new PushNotificationDevice();
resolvedEntity.PartitionKey = pk;
resolvedEntity.RowKey = rk;
resolvedEntity.Timestamp = ts;
resolvedEntity.ETag = etag;
resolvedEntity.ReadEntity(props, null);
resolvedEntity.PushPlatform = (DevicePlatform)props["PushPlatform"].Int32Value;
return resolvedEntity;
};
// Filter to only get the WNS and C2DM devices.
string wnsFilterCondition = TableQuery.GenerateFilterConditionForInt("PushPlatform", QueryComparisons.Equal, (int)DevicePlatform.Windows);
string c2dmFilterCondition = TableQuery.GenerateFilterConditionForInt("PushPlatform", QueryComparisons.Equal, (int)DevicePlatform.C2DM);
string combinedFilterCondition = TableQuery.CombineFilters(wnsFilterCondition, TableOperators.Or, c2dmFilterCondition);
TableQuery<PushNotificationDevice> query = new TableQuery<PushNotificationDevice>().Where(combinedFilterCondition);
TableQuerySegment<PushNotificationDevice> currentSegment = null;
List<PushNotificationDevice> deviceList = new List<PushNotificationDevice>();
Console.WriteLine("Fetching all Windows and C2DM devices");
while (currentSegment == null || currentSegment.ContinuationToken != null)
{
currentSegment = await devicesTable.ExecuteQuerySegmentedAsync(query, deviceResolver, currentSegment != null ? currentSegment.ContinuationToken : null);
deviceList.AddRange(currentSegment.Results);
}
Console.WriteLine("Generating batches to delete.");
foreach (PushNotificationDevice device in deviceList)
{
TableOperation deleteOperation = TableOperation.Delete(device);
string deviceType = device.PushPlatform.Equals(DevicePlatform.C2DM) ? "C2DM" : "Windows";
Console.WriteLine("Deleting a {0} device.", deviceType);
await devicesTable.ExecuteAsync(deleteOperation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment