2

I have a Phonegap/Cordova app that runs on iOS. It saves it's data into HTML5 localStorage.

I'm trying to work out if it's possible to sync the localStorage data (using iCloud) to other iOS devices, and even OS X.

From what I can see, in iOS localStorage is actually implemented as a SQLite database, which (when using Phonegap/Cordova) is written to the app's Documents directory:

Documents/Backups/localstorage.appdata.db

I also understand that there are three main ways of storing data in iCloud:

  • Key/Value storage
  • UIDocument / NSDocument
  • Core Data

I know I can't use the Key/Value iCloud storage method, because I have more than 1MB of data to store, and the limitation is 1MB per app with that method.

This question, I believe is talking about the UIDocument method, and asks if it is possible to store a SQLite db file in iCloud using that method. The answer is no because the database may become corrupted.

So that really leaves the Core Data method.

So my question is - would this work? Could I sync the localStorage.db file to iCloud using Core Data?

I've never used Core Data and don't know much about it. I'm just wondering if it would be possible, or if there is something else I don't understand.

Are there any other ways to sync localStorage data between iOS devices or OS X ?

4

1 回答 1

1

不幸的是,答案似乎是否定的,Core Data 不能与 HTML5 localStorage 一起使用

除了使用 Core Data 创建的 SQLite 数据库之外,Core Data 不能与 SQLite 数据库一起使用。如果你尝试这样做,你会在 XCode 中得到这个错误:

SQLite error code:1, 'no such table: Z_METADATA'

核心数据文档对此进行了解释:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-SW2

尽管 Core Data 支持 SQLite 作为其持久存储类型之一,但数据库格式是私有的。您不能使用原生 SQLite API 创建 SQLite 数据库并将其直接与 Core Data 一起使用(也不应该使用原生 SQLite API 操作现有的 Core Data SQLite 存储)

不过我还是想解决这个问题。我正在考虑创建一个反映 localStorage API 的 Javascript API。这将是一个 phonegap 插件,可以调用objective-c 代码,并有效地将其更改写入Core Data 数据库。然后核心数据数据库应该能够同步到 iCloud。

如果它有效,我会回来更新这个答案。

于 2012-11-10T14:16:45.977 回答