【问题标题】:iCloud: KeyValue Storage or Document Storage?iCloud:KeyValue 存储还是文档存储?
【发布时间】:2012-11-20 06:31:37
【问题描述】:

我是 iCloud 存储的相对初学者,在我构建下一个应用程序之前,我想了解一些关于什么会更好的意见。

这个应用程序将需要存储许多包含字符串数组的 NSDictionaries,仅此而已。将有 365 个字典(一年中的每一天一个),每个字典至少包含 8 个包含小字符串的数组。我知道这种数据类型可以存储在 key-value 中,但是我没有经验来判断这是否会超过 1mb 的限制。

所以我的问题是,对于上述场景,我应该在 icloud 上使用键值还是文档存储?

谢谢。

【问题讨论】:

    标签: iphone ios xcode ipad icloud


    【解决方案1】:

    如果可能超过最大限制,您应该考虑将字典存储在 PLIST 文件或核心数据中。

    假设您要使用 PLIST 文件。您可以将字典写入保存在文档目录中的文件。然后,使用下面的代码,您可以将 PLIST 文件从文档目录移动到 iCloud,它将同步到您的其他设备。

    当您的应用检测到 iCloud 中有 PLIST 文件的更新版本时(您可以检查 iCloud 中文件的修改日期,看看它是否比本地存储的更新),从 iCloud 复制并放置它在文档目录中。

    //find the URL of your app's ubiquitous container
    //setting URLForUbiquityContainerIdentifier to nil returns the URL for the first ubiquitous container in the list in your app's entitlements, you can replace nil with a string
    self.ubiquitousURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    
    //you may want to update self.ubiquitousURL to the documents folder in the ubiquitous container
    //self.ubiquitousURL = [self.ubiquitousURL URLByAppendingPathComponent:@"Documents"];
    
    //place the PLIST in iCloud
    [[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:plistURL destinationURL:[self.ubiquitousURL URLByAppendingPathComponent:@"file.plist"] error:NULL];
    
    //you have detected there is a new file in iCloud and want to copy it to the documents directory
    [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:[self.ubiquitousURL URLByAppendingPathComponent:@"file.plist"] error:NULL];
    [[NSFileManager defaultManager] copyItemAtURL:[self.ubiquitousURL URLByAppendingPathComponent:@"file.plist"] toURL:plistURL error:NULL];
    

    self.ubiquitousURL 是您的 iCloud 目录的 URL。 plistURL是documents目录下PL​​IST文件的URL。

    【讨论】:

    • 杰克,谢谢这真的是一个有用的答案。目前正在浏览 iCloud 文档设计指南。
    【解决方案2】:

    这里有很多选择。您可以使用应用程序的 Documents 目录来存储 PLIST、SQLite 数据库,或者更好的是,使用 Core Data。您实际上可以使用其中的任何一个,并且以后仍然可以集成 iCloud。无论您是否使用 iCloud,以上建议都是从最差到最好的顺序排列的。 PList 肯定是最简单的。

    如果您想了解有关 Core Data 和 iCloud 的更多信息,我强烈推荐斯坦福大学的 CS193P 课程,该课程可在 iTunes U 上免费获得。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2022-12-18
      相关资源
      最近更新 更多