mardi 31 mars 2015

Getting Pointer from Parse.com record in to SQLite table

Since a few days I've been using Parse.com to synchronize data to the cloud. I'm now writing all functions to fetch all data from the cloud and to synchronize it with a local SQLite database.


I fetch all the data from the cloud with a ParseObject like this:



var taskQuery = from TaskTable in ParseObject.GetQuery("Task")
where TaskTable.Get<Boolean>("Deleted") == false
where TaskTable.Get<DateTime>("updatedAt") > SqliteSyncDate
select TaskTable;
IEnumerable<ParseObject> tasks = await taskQuery.FindAsync();


After having it in the ParseObject I loop over it with a foreachbut then I'm having problems. I have a table Task which has a field UserId that I defined as a Pointer to the table Login. I now have to store this Pointer in my SQLite database but I have no clue how to 'parse' and/or save it locally now. My code:



//Loop over every record that is returned from Parse.com
foreach (ParseObject task in tasks)
{
try
{
//Create new record to add to Task
Tasks taskSqlite = new Tasks();
taskSqlite.Id = task.Get<int>("Id");
//This line fails! When I comment it out syncing works, otherwise it doesn't.
taskSqlite.UserId = task.Get<int>("UserId");
taskSqlite.Description = task.Get<string>("Description");
taskname = task.Get<string>("Description");
taskSqlite.Date = task.Get<DateTime?>("Date");
taskSqlite.Done = task.Get<Boolean>("Done");
taskSqlite.DoneBy = task.Get<string>("DoneBy");
taskSqlite.Deleted = task.Get<Boolean>("Deleted");
taskSqlite.LastModified = task.Get<DateTime?>("LastModified");

//And finally insert the record in to the SQLite database.
DATask.InsertTask(taskSqlite);
}
catch
{
Debug.WriteLine("SYNC FAILURE - failed on table Task on the task: " + taskname);
}


If I run this without the try catch I will get the following error:



03-31 16:37:52.676 I/MonoDroid( 4731): UNHANDLED EXCEPTION:
03-31 16:37:52.697 I/MonoDroid( 4731): System.NullReferenceException: Object reference not set to an instance of an object


So how exactly should I convert it or save it into the SQLite database? Would this be the correct way to link records to eachother?


Aucun commentaire:

Enregistrer un commentaire