concept sync in category android

appears as: sync, sync
Android in Practice

This is an excerpt from Manning's book Android in Practice.

An easy way to realize how valuable multitasking is on a computing device is to live without it. For some applications, this is no big deal—everything the application does is confined to the device anyways. An example of this might be a note-taking application. If all the app does is stores notes on your device, then you probably don’t care if it can multitask. But if your app stores your notes on a remote server so that they can be accessed (both read and write) from any computer/device, then multitasking starts to become nice. Why? If your app can run in the background, then it can keep the notes on the device and on the server in sync. Without this, you’ll need to resync with the server every time you launch the app. That may not sound like a big deal, but this is a network operation that could be slow. The user is going to experience this slowness every time they want to use the application. In fact, it may be the first thing they experience when they launch the application. Can you say bad user experience?

There are times when you need to guarantee that your file data is written to disk before moving on to other operations. For example, if you write file data in one process, and need to read that same file from another process, you’ll want to make sure the data is written before trying to read it. To guarantee the file is written immediately, regardless of the filesystem involved, you can manually call sync. Syncing ensures that the buffer catches up with the physical disk. You might be surprised to learn that FileOutputStream methods such as flush, write, and even close don’t ensure this, but it’s true.

The FileDescriptor object in Java is where you’ll find the sync method. FileDescriptor is a low-level handle to operations on the underlying machine-specific filesystem. You can get a FileDescriptor reference from FileOutputStream, and then sync, as shown in the next listing.