Matt Meyers
Matt Meyers

Reputation: 143

iCloud: KeyValue Storage or Document Storage?

I'm a relative beginner to iCloud storage and before I build my next app I wanted to get a few opinions on what would be better.

This app will need to store many NSDictionaries containing arrays of strings and nothing else. There will be 365 dictionaries (one for each day of the year) each with at least 8 arrays containing small strings. I'm aware that this data type can be stored in key-value, however I don't have the experience to judge whether that will exceed the 1mb limit.

So my question is, for the scenario described above, should I use key-value or document storage on icloud?

Thanks.

Upvotes: 0

Views: 1117

Answers (2)

Jack Humphries
Jack Humphries

Reputation: 13267

If it could exceed the maximum limit, you should consider storing your dictionaries in a PLIST file or core data.

Let's say you want to use a PLIST file. You can write your dictionaries to a file saved in the documents directory. Then, with the code below, you can move the PLIST file from the documents directory to iCloud, and it will be synced to your other devices.

When your app detects there is an updated version of the PLIST file in iCloud (you can check the modification date of the file in iCloud and see if it is newer than the one stored locally), copy it from iCloud and place it in the documents directory.

//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 is the URL of your iCloud directory. plistURL is the URL of the PLIST file in the documents directory.

Upvotes: 2

Adam Lockhart
Adam Lockhart

Reputation: 1195

You have lots of options here. You can use your app's Documents directory to store a PLIST, SQLite data base, or better yet, use Core Data. You can actually use any of these and still integrate iCloud later. And whether you use iCloud or not, the suggestions above are ordered from worst to best. PList would certainly be easiest.

If you want to learn more about Core Data and iCloud, I highly recommend Stanford's CS193P course, available free on iTunes U.

Upvotes: 0

Related Questions