我是 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/