Working on android Background Services and Hitting the API in recursion approx 100 times my UI got stuck. What is the Reason Behind That.
public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
sycTableData("A_Material");
Toast.MakeText(this, "from Srevice", ToastLength.Short).Show();
return StartCommandResult.NotSticky;
}
async void sycTableData(String table_name)
{
Boolean isSynced;
dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
"adodemo.db4");
bool exists = File.Exists(dbPath);
if (exists)
{
connection = new SqliteConnection("Data Source=" + dbPath);
connection.Open();
using (var contents = connection.CreateCommand())
{
contents.CommandText = "SELECT [_id], [tableName], [_count], [_isSynced] from [Items] where _id ="+tableCount;
var r = contents.ExecuteReader();
Console.WriteLine("Reading data");
count = Int32.Parse(r.GetValue(2) + "\t");
isSynced = (bool)r.GetValue(3);
Log.Info("valueis---->>>", count + "\t" + (bool)r.GetValue(3));
count = count++;
}
connection.Close();
using (HttpClient client = new HttpClient())
{
if (!isSynced)
{
string link = "http://52.66.58.148/Kbbs/Gets/getDataFromTable.php?databas=mowindemo&dbuser=root&dbpass=aRsZ01jRVHtC9BVv&server=localhost&table=" + table_name + "&paged=" + count;
Log.Info("Link is---->", link);
try
{
using (HttpResponseMessage response = await client.GetAsync(link))
{
using (HttpContent content = response.Content)
{
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
string mycontent = await content.ReadAsStringAsync();
Console.WriteLine(mycontent);
JObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(mycontent);
var properties = obj.Properties();
tableDataValues.Clear();
foreach (var prop in properties)
{
string key = prop.Name;
object value = prop.Value;
tableDataValues.Add(value.ToString());
}
if (tableDataValues[0].Equals("1"))
{
JArray jarr = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(tableDataValues[1]);
alldataValues.Clear();
allkeysValues.Clear();
for (int i = 0; i < jarr.Count; i++)
{
JObject obj2 = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(jarr[i].ToString());
var properties2 = obj2.Properties();
tableDataValues2 = new List<string>();
tablekeysValues2.Clear();
foreach (var prop2 in properties2)
{
string key2 = prop2.Name;
object value2 = prop2.Value;
if(value2.ToString().Contains("'"))
{
}
tableDataValues2.Add(value2.ToString());
tablekeysValues2.Add(key2);
}
alldataValues.Add(tableDataValues2);
allkeysValues.Add(tablekeysValues2);
}
connection.Open();
for (int i = 0; i < alldataValues.Count; i++)
{
String querymain = "INSERT OR IGNORE INTO" + " " + "[" + table_name + "]";
builder = new StringBuilder();
builder.Append(querymain + " " + "(");
for (int j = 0; j < alldataValues[i].Count; j++)
{
if (j > 0)
{
builder.Append("," + "[" + allkeysValues[i][j] + "]");
}
else
{
builder.Append("[" + allkeysValues[i][j] + "]");
}
//Log.Info("Data2 is" + i + " " + j, alldataValues[i][j]);
}
builder.Append(")" + " " + "VALUES" + " " + "(");
for (int m = 0; m < alldataValues[i].Count; m++)
{
if (m > 0)
{
builder.Append("," + "'" + alldataValues[i][m] + "'");
}
else
{
builder.Append("'" + alldataValues[i][m] + "'");
}
}
builder.Append(")");
Log.Info("sdasd--->>>>>", builder.ToString());
try
{
var commands = new[] { builder.ToString() };
var c = connection.CreateCommand();
c.CommandText = commands[0];
c.ExecuteNonQuery();
Console.WriteLine("\tExecuted " + commands[0]);
//connection.Close();
c.Dispose();
}
catch (Exception e)
{
Log.Info("Syntax Exception", e.Message);
}
}
count++;
if (count <= alldataValues.Count)//used for how much page data we want to sync of a table for testing purpose we take it 3
{
using (var contents = connection.CreateCommand())
{
contents.CommandText = "UPDATE Items "
+ "SET _count =" + count
+ " " + "WHERE _id =" + tableCount;
var r = contents.ExecuteReader();
Console.WriteLine("Reading data");
r.Close();
contents.Dispose();
}
}
else
{
using (var contents = connection.CreateCommand())
{
contents.CommandText = "UPDATE Items "
+ "SET _isSynced=" + "1" +
" " + "WHERE _id =" + tableCount;
var r = contents.ExecuteReader();
Console.WriteLine("Reading data");
contents.Dispose();
r.Close();
}
}
connection.Close();
sycTableData(table_name);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
Toast.MakeText(this, "Sync Complete", ToastLength.Long).Show();
}
}
connection.Close();
}
}