Skip to content

Commit

Permalink
Add thread syncronization
Browse files Browse the repository at this point in the history
To each DispatchCommandResult callbackId needs to be passed, so there is no loss of callbacks. When multiple cordova.exec() are called, due to their simultaneously execution error appears - "Failed to locate callback for id". This error is fixed via passing the callbackId and UI behaviour is as expected. Also there needs to be some kind of synchronization between executeSqlBatch and openDatabase, as the first cannot go without the other.
  • Loading branch information
nadyaA committed Feb 7, 2014
1 parent ebcfb3e commit 5af6842
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/wp8/SQLitePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
* Copyright (c) 2011, Microsoft Corporation
*/

using SQLite;
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Windows;
using System.Collections.Generic;
using SQLite;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using WPCordovaClassLib.Cordova;
using WPCordovaClassLib.Cordova.Commands;
using WPCordovaClassLib.Cordova.JSON;
using System.Text.RegularExpressions;

namespace Cordova.Extension.Commands
{
Expand Down Expand Up @@ -90,6 +87,7 @@ public class SQLiteQueryResult

#endregion
private string dbName = "";
private readonly AutoResetEvent signal = new AutoResetEvent(false);
private SQLiteConnection db;
//we don't actually open here, we will do this with each db transaction
public void open(string options)
Expand All @@ -109,17 +107,19 @@ public void open(string options)
return;
}

var callbackId = JsonHelper.Deserialize<string[]>(options)[1];
//if (!string.IsNullOrEmpty(dbOptions.DBName))
if (!string.IsNullOrEmpty(dbName))
{
//dbName = dbOptions.DBName;
System.Diagnostics.Debug.WriteLine("SQLitePlugin.open():" + dbName);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
this.dbName = dbName;
this.dbName = dbName;
signal.Set();
DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId);
}
else
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "No database name"));
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "No database name"), callbackId);
}
}
public void close(string options)
Expand All @@ -130,11 +130,23 @@ public void close(string options)
this.db.Close();

this.db = null;
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
var callbackId = JsonHelper.Deserialize<string[]>(options)[1];
DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId);
}
public void executeSqlBatch(string options)
{
List<string> opt = JsonHelper.Deserialize<List<string>>(options);

if (string.IsNullOrWhiteSpace(this.dbName))
{
signal.WaitOne(1000);
if (string.IsNullOrWhiteSpace(this.dbName))
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Database is not open!"), opt[1]);
return;
}
}

TransactionsCollection transactions;
SQLiteTransactionResult transResult = new SQLiteTransactionResult();
try
Expand Down Expand Up @@ -183,10 +195,10 @@ public void executeSqlBatch(string options)
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Error: " + e);
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), opt[1]);
return;
}
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, JsonHelper.Serialize(transResult)));
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, JsonHelper.Serialize(transResult)), opt[1]);
}
}
}

0 comments on commit 5af6842

Please sign in to comment.