문제

In my app I am using XML to store images drawn by user on canvas. After each figure is drawn I write the points to an xml file and store it in disk. The xml is fairly large. So it take a while before the write is finished. This is the code I am using for that.

private void  writeToXml( final Book book)
    {

        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    Serializer serializer = new Persister();
                    File result = new File(android.os.Environment.getExternalStorageDirectory() + "/MyBooks/Book.xml");

                    try {

                            serializer.write(book, result);


                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                } finally {
                    //updateBook();
                }
            }
        };
        t.start();

    }

The issue for me is if the user quits the app before the write is finished the XML file will be corrupted. How can I do this better. How can I ensure that even the app is closed the file will be properly written?

Thanks

도움이 되었습니까?

해결책

The issue for me is if the user quits the app before the write is finished the XML file will be corrupted.

That depends a bit upon your definition of "quits the app".

Pressing HOME, pressing BACK out of your activities, and similar things should not prevent your existing thread from running to completion, assuming that it will be done in a second or two. If you are getting corrupt results here, you should be making sure that you are not messing around with your Book instance while it is being saved.

If "it take a while before the write is finished" is going to take more than a second or two, it is possible that Android is terminating your process before the write is completed. The corruption is because your disk-writing code appears to be writing to the real file, and that should only be done the first time you use that file. Second and subsequent times for saving should be written to a temporary file first, then renamed to be the real file. This would avoid the corruption, though the save would effectively not happen.

To make this even more reliable, use an IntentService instead of a thread, as that will indicate to Android that you are still doing work and should give you a few more seconds of uninterrupted time.

If "it take a while before the write is finished" is going to exceed 15 seconds, now you have to consider the possibility that the device will fall asleep before your disk I/O is complete. To combat that, either use my WakefulIntentService or the Android Support package's WakefulBroadcastReceiver.

And bear in mind that if the user "quits the app" by clicking Force Stop in settings, your process is terminated immediately. And there are other calamities, like the device running out of power, that could occur. That is why you need to make sure that you write to a temporary file first, to reduce the odds that such an event corrupts your data.

다른 팁

You can add your call to writeToXml() function inside a normal thread. This will ensure that even after the application is minimized or closed, the thread will complete its task and only then terminate.

You can start another thread within it. It can look something like this:

    //Inner thread
Thread t2=new Thread(new Runnable() {
    public void run()
    {
            writeToXml(book);
}); t2.start(); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top