iphone - iCloud:KeyValue 存储还是文档存储?

标签 iphone ios xcode ipad icloud

我是 iCloud 存储的新手,在构建我的下一个应用程序之前,我想听听一些关于什么会更好的意见。

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

那么我的问题是,对于上面描述的场景,我应该使用icloud上的键值存储还是文档存储?

谢谢。

最佳答案

如果它可能超过最大限制,您应该考虑将您的词典存储在 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 是文档目录中 PLIST 文件的 URL。

关于iphone - iCloud:KeyValue 存储还是文档存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672292/

相关文章:

iphone - Objective-C : Multiple delegates

ios - 如何在 IOS 上对通知(如 Whatsapp)进行分组

ios - XCode 无法识别 iPad Storyboard

ios - 如何正确调整 TableView 单元格的大小?

ios - 识别字符串中是否有不需要的字符

ios - Swift - 闭合模式

iPhone将字符串分成多行并显示为标签

ios - 使用 Facebook 的 FirebaseSimpleLogin 后,Firebase observeEventType 没有触发?

ios - 在单元格 UICollectionView 中插入/删除部分

iphone - 将分段控件添加到 UINavigationBar 的中心