在 iPhone 上保存 XML 文件的最佳方式?

发布于 2024-09-24 06:17:48 字数 496 浏览 0 评论 0原文

我使用 MonoTouch 和 System.Data 创建一个 DataSet(对于那些不熟悉的人来说只是 xml)以进行简单的数据绑定。我的应用程序上的数据很少,因此无需全力以赴使用 SQLLite。数据集的使用可以轻松地通过网络服务进行云同步。

我在保存时将数据集序列化到个人文件夹,当然,当应用程序启动以加载用户数据时读取此文件。我遇到过此文件已损坏的问题,但我不确定原因。我认为这些设备上的文件 I/O 可能很慢,这可能是原因,我不确定,但它正在发生。

我还担心,当用户将其设备与 iTunes 同步时,iTunes 可能会在 PC/MAC 之间来回传递此文件,这可能是损坏的原因?

我想阻止此设备文件与 iTunes 同步并可靠地保留它。我正在使用 NSFile.Save 选项将其保存到设备。我在想,既然它是一个文本文件,也许我可以更安全地将它存储在标准用户设置区域中?我想这会阻止它被itunes同步?

处理 xml 数据集存储的文件 I/O 的最可靠、最安全的方法是什么?

谢谢。

I'm using MonoTouch and also System.Data to create a DataSet (just xml to those not familiar) for simple data binding. Data on my app is minimal so no need to go all out with SQLLite. The dataset use makes it easy to pass via web services for cloud sync.

I serialize the DataSet to the personal folder on save and of course read this file when the app starts up to load up the user's data. I've had issues where this file is becoming corrupt and I'm not sure why. I assume file I/O may be slow on these devices and that could be the cause, I'm not sure, but it is happening.

I'm also concerned that maybe iTunes is passing this file back and forth between the PC/MAC when the user syncs their devices with iTunes, which may be the cause of the corruption?

I want to prevent this device file from syncing with iTunes and also reliably persist it. I'm using the NSFile.Save option to save it to the device. I'm thinking since it's a text file maybe I could more safely store it in the standard user settings area instead? This would prevent it from being synced by itunes, I presume?

What is the most reliable and safe way to handle this file i/o for the xml dataset storage?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多像笑话 2024-10-01 06:17:48

您正在使用 MonoTouch。这不是简单地使用准备写入 Documents 文件夹中的文档的 FileStream 对象调用 DataSet.WriteXml() 吗?

该 Documents 文件夹已备份到 iTunes。它没有同步,但如果您的用户正在恢复他们的手机(因为他们把手机变砖,丢失了,等等),它会有所帮助。它没有解释为什么它会腐败。

我唯一能想到的原因是它损坏的原因是您的应用程序编写它花费了太长时间。从用户退出应用程序到关闭应用程序之间有一个有限的时间,以防止应用程序劫持系统并恶化用户体验。

如果写入整个数据集花费的时间太长,您需要考虑将其最小化。也许您可以只存储数据,而不存储模式。或者,您可以设计一种方法来仅存储退出时的增量,并在用户再次加载您的应用程序时进行协调。

您还可以通过写入第二个文件来防止数据完全丢失,并在该操作完成后删除旧文件并重命名。这样,下次启动时,如果写入操作未完成,旧文件仍然存在,用户只会丢失最近的更改。

无论如何,如果您的数据太大而无法完成简单的写入操作,您应该考虑不同的选项,例如 sqlite。

You're using MonoTouch. Isn't it simply a matter of calling DataSet.WriteXml() with a FileStream object ready to write to a document in your Documents folder?

That Documents folder is backed up to iTunes. It's not synced, but it helps if your user is restoring their phone (because they bricked it, lost it, whatever). It doesn't explain why it's corrupt.

The only thing that I can think of why it's corrupt is because it took too long for your app to write it. There's a limited time from the point where the user exits the app until it's closed down, to prevent apps from keeping the system hostage and deteriorate user experience.

If writing the whole dataset takes too long, you want to think about minimizing that. Perhaps you can just store the data, and not the schema. Or you can devise a way to store only the deltas on exit and reconcile when the user has loaded your app again.

You can also prevent complete loss of data by writing to a second file, and when that operation completes delete the old file and rename. That way, the next time you start up if the write operation didn't complete, the old file would still be there and the user would have only lost their more recent changes.

In any case, if your data gets too big for a simple write operation to complete, you should look at different options such as sqlite.

凉城凉梦凉人心 2024-10-01 06:17:48

最好的选择可能是将 XML 保存为文本。它就像 File.WriteAllText(...) 一样简单 - 没有理由为此转到 NSFile 。这是 MonoTouch 优势的一部分:)

关于同步,规则如下:

  • 如果将文件保存在用户的文档文件夹中(Environment.SpecialFolder.MyDocuments 和 Environment.SpecialFolder.Personal 都指向用户的文档文件夹),那么它就会同步每当用户与 iTunes 同步时进行备份。

这没有什么问题。它可以在会话之间保留数据,并在用户的手机出现问题并且需要从备份中恢复时使数据可以恢复。由于您的问题是关于在手机上保留 XML 文件,这就是您想要的。

至于 iTunes 问题,速度和同步没有问题,因为手机同步时您的应用程序不会运行。该文件要么已保存,要么不保存。发生的任何损坏都是在您的应用运行时发生的。

文件损坏的原因可能包括:

  • 用户退出前未保存。您有机会执行此操作。

  • 没有优雅地处理来电。系统也会就此向您发出警告。

iTunes 绝对不会损坏您的文件。如果是这样的话,iOS 应用程序就会全部崩溃。不管出于什么原因,它都可能发生在你的开发机器上,但我从未在其他地方见过这种情况发生,而且我已经完成了大量的 iOS 开发。

如果您想要有关读写文件的教程,我发布了 在另一个问题中回答

虽然很长,但重点是尽可能多地回答问题,这样就不会有人感到困惑或困惑。

iOS 设备的一大好处是,您(对于大多数应用程序)又回到了一次一个人的世界。您编写的应用程序无需担心 5,000 人同时尝试使用您的基于 Web 的应用程序(这并不总是正确的,但是......您明白了)。因此,您可以执行通常认为对性能不利的操作,但不太可能看到任何性能问题(只要您保存的文件足够小,可以快速保存,或者您正在保存在另一个线程的后台 - 你永远不想用繁重的 IO 操作阻塞主(UI)线程)。

如果您还有其他疑问,请随时提问。

我希望这有帮助:)

Your best bet is probably to just save the XML as text. It's as simple as File.WriteAllText(...) - there's no reason to go to NSFile for this. That's part of the advantage of MonoTouch :)

Regarding syncing, here's the rule:

  • If you keep the file in the user's documents folder (Environment.SpecialFolder.MyDocuments and Environment.SpecialFolder.Personal BOTH point to the user's doc folder), then it's going to get backed up whenever the user syncs with iTunes.

There's nothing wrong with this. It persists the data between sessions and makes it recoverable if something goes wrong with the user's phone and they need to restore from a backup. Since your question is about persisting an XML file on the phone, this is what you want.

As for the iTunes question, there's no problem with speed and syncing because your app isn't going to be running while the phone is syncing. The file will either have been saved or it won't. Any corruption that takes place is happening while your app is running.

Reasons for files getting corrupted can include:

  • Not saving before the user quits. You get a chance to do this.

  • Not gracefully handling an incoming phone call. The system warns you about this as well.

iTunes definitely isn't corrupting your file. If that were the case, iOS apps would all be broken. It could be happening on your dev machine for whatever reason, but I've never seen this happen elsewhere, and I've done quite a bit of iOS development.

If you'd like a tutorial on reading and writing files, I posted an answer in another question.

It's lengthy, but the point was to answer as many questions as I could so nobody would be left hanging or confused.

A nice thing about iOS devices is that you're back (for most apps) in the one-person-at-a-time world. You're writing apps where you don't have to worry about 5,000 people trying to use your web-based app at the same time (that's not always true, but... you get the point). As a result, you can do things that you might normally consider bad for performance, but you're unlikely to see any performance problems (as long as the file you're saving is either small enough to be saved quickly or you're saving in the background on another thread - you never want to block the main (UI) thread with a heavy IO operation).

If you have any more questions, feel free to ask.

I hope this helps :)

红尘作伴 2024-10-01 06:17:48

很多框架都是只读的,但我发现来自 http:// /code.google.com/p/gdata-objectivec-client/ 读/写效果很好。

话虽如此,在 iPhone 上使用带有 SQLLite 后端的 Core Data 会给自己带来很大帮助。 :-) 苹果已经为你完成了这一切,并且比我们任何人都更优化它,

干杯

尼克

Lots of frameworks are read-only, but I've found that GDataXMLNode from http://code.google.com/p/gdata-objectivec-client/ works very well read/write.

Having said that, on the iPhone you'd do yourself a big favour using Core Data with a SQLLite backend. :-) Apple has done all this for you and optimized it more than any of us every will

Cheers

Nik

呆° 2024-10-01 06:17:48

考虑使用 SQLite,我会选择类似

entify - 如果它按照它所说的那样做 - 看起来真的很好。

在 iPhone 上持久保存 XML 作为存储和访问数据的一种方式是您不想陷入的麻烦。我在这里写了http://iwayneo.blogspot .com/2010/08/festival-star-history-serialization-as.html

Consider using SQLite, I'd go for something like

entify - if it does what it says it can do - looks really good.

persisting XML on the iPhone as a means to store and access data is a nause you dont want to get into. I wrote about it here http://iwayneo.blogspot.com/2010/08/festival-star-history-serialization-as.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文