Delete File after Sync

Dec 2, 2013 at 10:56 AM
Edited Dec 5, 2013 at 11:16 AM
It is possible to update Files / delete Files after Sync?

Message: Unable to delete file.
Coordinator
Dec 6, 2013 at 12:21 PM
I'm sorry, but I don't understand your question.
Can you explain more in détails what is your problem ?
Dec 10, 2013 at 7:34 AM
Yes.
Start App->start Synchronisation.After Sync, i tryed to delete the database.
The result is the message: Unable to delete file.

Delete is possible after restart the App.
Coordinator
Dec 10, 2013 at 10:09 AM
Why do you want to delete the database after a synchronization ? I really don't understand what kind of behavior you want :)
Anyway, it's not a sync problem you face here, I think
Dec 11, 2013 at 8:19 AM
I don t know.It s not my task :D
But thanks.
Dec 16, 2013 at 10:27 AM
Sometimes it is necessary to delete the database file after sync i.e. if a process somehow left the database in a corrupt state that can't be reset easily.
Store apps can't automatically be restarted hence it is not possible without user interaction to delete the file - at least the user has to restart the app manually and a delete flag has to be stored before the app quits (i.e. in a config file) to indicate that the app has to delete the database at startup..
It would be easier and more comfortable if the database could simply be deleted as soon as the last connection to it was disposed.
Coordinator
Dec 16, 2013 at 2:19 PM
Here is the code I m using to delete the database, if required :
        public async Task DeleteDatabase()
        {
            // Get the isolated storage file for the application.
            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

            var exist = await applicationFolder.FileExistsAsync(this.DatabaseName);

            if (exist)
            {
                StorageFile databaseFile = await applicationFolder.GetFileAsync(this.DatabaseName);
                await databaseFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
            }
        }
And here is the Re Sync code i m using :
               var messageDialog = new MessageDialog("The specified offline schema does not match database offline. Do you want to re sync the application totally ?");

                // Add commands and set their callbacks
                messageDialog.Commands.Add(new UICommand("Yes", async (command) =>
                {
                    // Delete the ticket
                    await DataService.Current.DeleteDatabase();

                    this.SyncCommand.Execute(null);

                }));

                messageDialog.Commands.Add(new UICommand("No", (command) =>
                {
                }));

                // Set the command that will be invoked by default
                messageDialog.DefaultCommandIndex = 0;
                messageDialog.CancelCommandIndex = 1;

                // Show the message dialog
                await messageDialog.ShowAsync();
Dec 16, 2013 at 2:46 PM
that does work if the app didn't sync since the last start, and it doesn't work after it synced until restart.
So it's the same as described above.
Coordinator
Dec 16, 2013 at 9:07 PM
I have made the test, after a sync process, and it worked
Check the Sample02 in the source tab, where I did it.

let me know it solved your problem
by the way, you can't delete the database if there is any connection opened to the database. You have to be sure of that

Seb
Dec 18, 2013 at 9:10 AM
I have tryed many times but it never solved the problem. Error: Unauth. Access Exception
Have u tryed it with SQliteConnection?
  • how to close the Connection? The only one is
    var result = await Context.Current.Sync();
Dec 18, 2013 at 9:40 AM
Same for me.
Before the call to the sync function the database is free and can be deleted.
Sync is called and after it is finished the context is closed and disposed but the database remains locked.
//create context
myEntity.myEntityOfflineContext entity= new myEntity.myEntityOfflineContext(dbPath, uri));

//now the database is still free and could be deleted
status = await entity.SynchronizeAsync();

//now the database is locked
entity.Close();
entity.Dispose();

//the database is still locked and remains locked even if the calling function was left.
How can I close the connection?
Coordinator
Dec 18, 2013 at 11:06 AM
That's strange, because I can't reproduce your behavior on my test Platform.

I have made an update of the sample HelloWorld (see the Source code tab, under Samples directory)
Can you test it and say to me if it's work for you ?

Here is the code I made to test :
            Uri serviceUri = new Uri("http://localhost/Sample.WebServer/DefaultScopeSyncService.svc");
            DefaultScope.DefaultScopeOfflineContext ctx = new DefaultScopeOfflineContext("fabrikam.db", serviceUri);
            
            var stats = await ctx.SynchronizeAsync();

            if (stats.Error != null)
            {
                Debug.WriteLine(stats.Error.Message);
                return;
            }

            await DeleteDatabase();

            ctx = new DefaultScopeOfflineContext("fabrikam.db", serviceUri);
            stats = await ctx.SynchronizeAsync();

            if (stats.Error != null)
            {
                Debug.WriteLine(stats.Error.Message);
                return;
            }

            Debug.WriteLine(stats.TotalDownloads);
And the delete method :
        public async Task DeleteDatabase()
        {
            // Get the isolated storage file for the application.
            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

            var files = await applicationFolder.GetFilesAsync();

            var exist = files.Any(f => f.Name == "fabrikam.db");

            if (exist)
            {
                StorageFile databaseFile = await applicationFolder.GetFileAsync("fabrikam.db");
                await databaseFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
            }
        }
By the way, do you have the problem on Windows 8 application or Windows Phone 8 application ?

Seb
Dec 18, 2013 at 3:22 PM
Windows Phone 8

I think, the problem is the server.Because your database is local.
Maybe there is another way to unlock files from isolated Storage?
Coordinator
Dec 18, 2013 at 3:34 PM
I don't understand why you think that the problem is the server ????
The server is running SQL Server and there is no reason that the server would block the SQLite client database...

For Windows Phone 8, I will make a test
Dec 18, 2013 at 3:53 PM
Thanks.

yes, u are right but i don t see more solutions.
Jan 9, 2014 at 2:35 PM
Edited Jan 14, 2014 at 12:09 PM
The Connection is still open 10-30 sec. after synchronisation.
Therefore the connection isn t the problem.

Maybe the database is locked.But how unlock database?

EDIT: It s the ConnectionPool.Where to reset the Pool?
EDIT: It s not the Connectionpool, seeting false in Connection String.

GC.Collect / GC.WaitForPendingFinalizers(); isn t helping too.


EDIT:
The Problem is the class: HttpCacheRequestHandlerAsync

Test:
  • WLAN Connection off
  • After Sync, an Error occured: "The Remote Server cannot...."
  • You can t delete database now, therefore set "webRequest.Abort()" in the Catch Block WebException
  • now it s possible to delete Database, but i don t know, what to do without an error (if Sync is possible)
Feb 20, 2014 at 10:31 AM
Another option: Clear the content of isolatedStorage.

IsolatedStorageFile.GetUserStoreForApplication().Remove();
It works for me but this deletes all files.