【问题标题】:Getting Pointer from Parse.com record in to SQLite table从 Parse.com 记录中获取指向 SQLite 表的指针
【发布时间】:2015-03-31 14:41:13
【问题描述】:

几天以来,我一直在使用Parse.com 将数据同步到云端。我现在正在编写所有函数以从云中获取所有数据并将其与本地 SQLite 数据库同步。

我使用 ParseObject 从云端获取所有数据,如下所示:

 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();

ParseObject 中有它之后,我用foreach 循环它,但后来我遇到了问题。我有一个表Task,其中有一个字段UserId,我将其定义为Pointer 到表Login。我现在必须将这个 Pointer 存储在我的 SQLite 数据库中,但我现在不知道如何“解析”和/或在本地保存它。 我的代码:

//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);
            }

如果我在没有 try catch 的情况下运行它,我会收到以下错误:

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

更新:正如所指出的,我决定也打印异常。这是例外:

Exception: System.InvalidCastException: Value is not a convertible object:      
Parse.ParseObject to System.Int32
03-31 17:03:18.335 I/mono-stdout( 8270): SYNC FAILURE - failed on table Task
on the task:  Exception: System.InvalidCastException: Value is not a 
convertible object: Parse.ParseObject to System.Int32

[0:] SYNC FAILURE - failed on table Task on the task:  Exception:
System.InvalidCastException: Value is not a convertible object:
Parse.ParseObject to System.Int32
at System.Convert.ToType (System.Object value, System.Type conversionType,
IFormatProvider provider, Boolean try_target_to_type) [0x00000] in <filename
unknown>:0 
at System.Convert.ChangeType (System.Object value, System.Type
conversionType) [0x00000] in <filename unknown>:0 
at Parse.ParseClient.ConvertTo[Int32] (System.Object value) [0x00000] in
<filename unknown>:0 
at Parse.ParseObject.Get[Int32] (System.String key) [0x00000] in <filename
unknown>:0 

然后我创建了一个ParseRelation。但是我现在如何才能从中获取 UserId 呢?

ParseRelation<Parse.ParseObject> relatie = 
task.GetRelation<Parse.ParseObject>("UserId");

那么我应该如何将它转换或保存到 SQLite 数据库中呢?这是将记录相互链接的正确方法吗?

【问题讨论】:

  • 你为什么不catch (exception ex) 看看异常是什么?
  • 好点。好吧,所以它期待 ParseRelation。我现在有了这个,但我不知道如何从中取出一个项目(UserId)
  • 我建议您查看 API 文档中的 here 以获取有关如何从您的 ParseObject 中提取用户 ID 的线索
  • 似乎有一个 getQuery 选项,但我没有看到任何示例,也没有看到我应该如何查询和/或获取 ouf 的值。parse.com/docs/android/api/com/parse/ParseRelation.html

标签: c# sqlite parse-platform xamarin


【解决方案1】:

您似乎遇到了 int 类型的问题。也许试试 Int32 什么的?如果这没有帮助,您可以检查数据库中的值。也许 UserId 不是可以转换为 int 的数值?

【讨论】:

  • 感谢您的回答。原来它是作为 ParseObject 从云数据库返回的,然后您必须以某种方式对其进行转换..
【解决方案2】:

抱歉回复晚了

taskSqlite.UserId = task.ObjectId;

应该做的伎俩

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2014-10-22
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多