在线数据库发生变化时如何将iPhone本地数据库同步到在线数据库?
目前,每当需要数据时,我的应用程序都会使用 SOAP 通过 ASP.NET XML Web 服务从 Microsoft SQL 2005 数据库中提取数据。
我想更改我的应用程序,以便它有一个本地数据库,并且该数据库将检查数据是否已更改并将其自身同步到任何新数据。
如何判断是否有新数据?
我是否只计算 SQL 表中的行数,如果它小于 CoreData 数据库中的行数,则重写 CoreData 数据库?
有没有办法告诉只同步已更改或添加/删除的记录?
我应该在应用程序启动时同步所有数据还是仅同步与加载视图相对应的表?
非常感谢任何帮助!
谢谢, -麦克风
Currently my app pulls data from an Microsoft SQL 2005 database via ASP.NET XML web services using SOAP whenever it needs data.
I would like to change my app so that it has a local database, and this database would check to see if the data has changed and synch itself to any new data.
How do i tell if there is any new data?
Do i just count the number of rows in the SQL table and if it less than the number of rows in the CoreData database then re-write the CoreData database?
Is there a way to tell only sync the records that have changed or been added/deleted?
Should i be synching all data when the app starts or synching only the table that corresponds to the loading view?
Any help is greatly appreciated!
Thanks,
-Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的架构设计很聪明,那么您就可以。最好的选择可能是将“created_at”和“updated_at”日期字段添加到数据库表中(并在修改或创建记录时设置它们)。然后在同步请求查询中,选择日期大于本地数据库中最高日期的所有记录。这将适用于单向同步。双向同步要困难得多,需要考虑接触解析。
至于同步,我建议尽可能在后台进行。当获取新记录时,您应该能够使用 NSFetchedResultsController 来处理视图的更新。这样用户就不会遇到任何烦人的加载屏幕。
If you are clever with your schema design than you can. The best option is probably to add 'created_at' and 'updated_at' date fields to your database tables (and set them whenever you modify or create records). Then in your sync request query, select all records that have a date greater than the highest date in your local database. This will work for a ONE way synchronization. TWO way synchronization is much more difficult and requires thought on contact resolution.
As for syncing, I'd recommend doing it in the background whenever possible. You should be able to use the
NSFetchedResultsController
to handle updating your views when new records are fetched. This way the user won't experience any annoying loading screens.您需要一种方法来检测自上次连接以来数据库中发生的更改。 SQL Server 为此提供了两个框架:更改跟踪 和 更改数据捕获。有关两者的比较,请参阅比较更改数据捕获和更改跟踪 。 .Net 同步框架利用这些框架来自动执行数据同步。
不幸的是,这些功能在 SQL Server 2005 中都不可用。它们都是 SQL Server 2008 的功能,因此您必须升级。
使用 SQL Server 2005,您只能手动跟踪更改。有几种方法,但都不完美,它们都围绕触发器和跟踪表,或者围绕使用 TIMESTAMP 跟踪行版本(问题始终是如何跟踪已删除的行)。
虽然 Core-Data 内置了同步支持(请参阅 同步核心数据应用程序),它不适用于您的场景。
You need a way to detect what has changed in the database since you last connected. SQL Server offers two frameworks for this: Change Tracking and the Change Data Capture. For a comparison of the two, see Comparing Change Data Capture and Change Tracking. These frameworks are leveraged by the .Net Sync Framework to automate data synchronization.
Unfortunately, none of these are available in SQL Server 2005. They are all SQL Server 2008 features, so you would have to upgrade.
With SQL Server 2005 you can only track the changes manually. There are several approaches, all imperfect, and they all revolve around either triggers and tracking tables, or around tracking row versions with a TIMESTAMP (the problem is always how to track deleted rows).
While Core-Data has buil-in support for synchronization (see Syncing Core Data Applications), it is not usable with your scenario.