Search code examples
windows-8async-awaitstoragefile

Do I need to call CachedFileManager.DeferUpdates in Windows 8 app


In the file picker Windows 8 sample a file is saved like this:

CachedFileManager.DeferUpdates(file);
await FileIO.WriteTextAsync(file, stringContent);
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);

I'm serialising an object as XML so doing it slightly differently:

// CachedFileManager.DeferUpdates(file);
var ras = await file.OpenAsync(FileAccessMode.ReadWrite);
var outStream = ras.GetOutputStreamAt(0);
var serializer = new XMLSerializer();
serializer.Write(myObject, outStream);
// FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);

It works with or without the CachedFileManager (commented out above).

So, should I include the CachedFileManager and if I do use it am I saving the file in the right way.

This code works and saves the file fine, but I don't like including code that I don't understand.


Solution

  • Yes, this code will work without CachedFileManager. But, when you use CachedFileManager, you inform the file provider that the file is in process of change. If your file is located on SkyDrive it is faster to create a file and upload it at once instead of update it multiple times.