[英]In Android ContentProvider backed by SQLite, how should I implement insertOrUpdate semantics?
This is in the case where there is a UNIQUE column apart from the _id column. 在_id列之外还有UNIQUE列的情况下。
I can see three options: 我可以看到三个选项:
Leave insert as insert-if-no-conflict, and update as update-if-present (this seems to be the "standard"), and perform all the work on the client to decide how to resolve this. 将insert保留为“ if-if-no-conflict”,将更新保留为“ update-if-present”(这似乎是“标准”),并在客户端上执行所有工作,以决定如何解决此问题。
Change insert to insert-or-update-if-present and leave update as update-if-present. 将insert更改为insert-or-update-if-present,将update保留为update-if-present。 Does this break the standard ContentProvider contract?
这会违反标准的ContentProvider合同吗?
Change update to update-or-insert-if-not-present and leave insert as insert-if-no-conflict. 将update更改为update-or-insert-if-present,然后将insert保留为insert-if-no-conflict。 This makes less sense than the second option above to me, so I'll probably not do this - but I suspect it's pretty similar in the end.
这对我来说比上面的第二个选项没有意义,因此我可能不会这样做-但我怀疑最后它还是很相似。
Additionally, if I do the first option, is there any advantage in doing 另外,如果我做第一个选择,这样做有什么好处吗?
I anticipate more updates than replaces overall, but am mainly concerned with making this maintainable, as I don't expect this to be a performance bottleneck. 我预计将有更多更新而不是整体替换,但主要是要使它具有可维护性,因为我认为这不会成为性能瓶颈。
My inclination will be to query and then update or insert based on if there is an existing record. 我的倾向是根据是否有现有记录来查询然后进行更新或插入。 You can use the existing ContentProvider methods for this - and it will be quite readable for subsequent developers to understand.
您可以为此使用现有的ContentProvider方法-以后的开发人员将很容易理解。
The downside is there's a couple of unnecessary DB hits. 缺点是有一些不必要的数据库命中。
The article talks through how you could modify the ContentProvider to 'implement' insertOrUpdate 本文讨论了如何将ContentProvider修改为“实现” insertOrUpdate。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.