locked
When and how to save. RRS feed

  • Question

  • I have a program the uses SQLite database to save app files. The most stable saving method I have is to manually click save button, other methods such as waiting for Suspension Event results in an unknown result (sometimes it stops saving at some random point thus only the "early" part of my file are saved). Here is my saving method:

    		if (this->FlipViewCardDeck->Items->Size == 0) return;
    		this->_cardDeck.getCards()->clear();
    		for (int i = 0; i < FlipViewCardDeck->Items->Size; i++){
    			CCard* card = new CCard();
    			((CardView^) this->FlipViewCardDeck->Items->GetAt(i))->getCard(card);
    			this->_cardDeck.addCard(card);
    		}
    	
    		Database db = Database();
    		db.init("test.db");
    		int id = db.insertCardDeck(_cardDeck);
    		_cardDeck.setId(id);
    		db.destroy();

    I even tried wrapping it with

    Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Low, ref new Windows::UI::Core::DispatchedHandler([this](){
    
      // Saving method from above here.
      })); 

    but it made the unstable problem worse. The only way to ensure a solid save is to click the save button, which is a pain.

    My Question is, in what event is it the best time to save, and how do I do it asynchronously?

    Tuesday, February 18, 2014 9:45 PM

Answers

  • Hi,

    If you want to save data or file asynchronously, you should consume an asynchronous method by using the task class that's defined in the concurrency namespace in ppltasks.h.  Refer to Asynchronous programming in C++ to get more information.  

    I do not why your file can not be saved unstable in Suspending event. May be your file is very big so you should have a lot of time to save it. But the Suspending event handler has only 5 seconds to complete its operation. You can try these codes below:

       void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
    {
        (void) sender; // Unused parameter
        assert(IsMainThread());
    
        auto deferral = e->SuspendingOperation->GetDeferral();
        ..........//asynchronous task to save the file 
      deferral->Complete();//asynchronous task to save the file
    
    }
    
    
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by JoeyAndres Thursday, February 20, 2014 9:06 PM
    Thursday, February 20, 2014 8:45 AM