2

I am developing iPhone app,i have one doubt

I have an NSMutableDictionary which contains data in this format

dict at 0th index:

"ProductId":"77386",
"ProductImage":"http://static.abcd.com/images/product/large/design_gallery_slide_green - Copy (2).jpg",
"Productshortname":"Apple iPhone 5c 16GB",
"categorycode":null,
"categoryid":8,
"categoryimage":"",
"categoryshortname":"",
"favorite":"0",
"price":"31500",
"productnameinUrl":"apple-iphone-5c-16gb",
"storecount":"10"

dict at 1st index:

    "ProductId":"11386",
    "ProductImage":"http://static.abcd.com/images/product/large/design_gallery_slide_green - Copy (2).jpg",
    "Productshortname":"Apple iPhone 5s 16GB",
    "categorycode":null,
    "categoryid":8,
    "categoryimage":"",
    "categoryshortname":"",
    "favorite":"1",
    "price":"31500",
    "productnameinUrl":"apple-iphone-5s-16gb",
    "storecount":"18"

dict at 2nd index:

    "ProductId":"31386",
    "ProductImage":"http://static.abcd.com/images/product/large/design_gallery_slide_green - Copy (2).jpg",
    "Productshortname":"Apple iPhone 4s 16GB",
    "categorycode":null,
    "categoryid":8,
    "categoryimage":"",
    "categoryshortname":"",
    "favorite":"1",
    "price":"31500",
    "productnameinUrl":"apple-iphone-4s-16gb",
    "storecount":"38"

and so on...

What i want to do is, i want to store this dictionary indexes some where in my directory and i want to fetch it after some time or even after closing and opening the app after few times.

where should i store this kind of data ? is there any storage for strong this kind of data?

Please help and thanks in advance !!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Krunal
  • 6,440
  • 21
  • 91
  • 155

7 Answers7

3

You can store the data in NSUserdefaults and can access any time and anywhere as you want

yourdict;//Your NSDictionary Object That contains the data to store

[[NSUserDefaults standardUserDefaults] setObject:yourdict forKey:@"dict"];

[[NSUserDefaults standardUserDefaults] synchronize];

At the time of retrieval of data,

dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"dict"]; 
Shekhu
  • 1,998
  • 5
  • 23
  • 49
  • You going to share how? Otherwise this is just a comment – Popeye Jul 15 '14 at 07:22
  • Done with exemple @Popeye – Shekhu Jul 15 '14 at 07:26
  • +1 for the edit. But could you please put some context to `yourDict;` it just seems to be a floater at the top of your code. – Popeye Jul 15 '14 at 07:30
  • Can i get path of `NSUserDefaults` ? and does it get destroy when i close my app ? – Krunal Jul 15 '14 at 07:35
  • No Need for the Path .Do check the Documentation for this https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsuserdefaults_class/reference/reference.html – Shekhu Jul 15 '14 at 07:39
  • does it get destroy when i close my app ? – Krunal Jul 15 '14 at 07:46
  • No,as it holds the data ,once user closes the app,until you replace or remove the object – Shekhu Jul 15 '14 at 07:48
  • `[[NSUserDefaults standardUserDefaults] setObject:[array objectAtIndex:indexPath.row] forKey:@"MOBILES"];` when i write this it crashes my app shows **attempt to insert non-property list object** – Krunal Jul 15 '14 at 07:59
  • Hey You can save the NSDICtonary object here,And can retrieve the object where you want and then iterate from that retrieved object to get the data – Shekhu Jul 15 '14 at 08:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57311/discussion-between-krunal-and-aiden). – Krunal Jul 15 '14 at 08:01
  • @Krunal see my answer with the disadvantages to using the NSUserDefaults – SomeGuy Jul 15 '14 at 09:15
2

You've already chosen an approved answer but I'll throw my thoughts in anyway.

This information looks like it could get large.

The user defaults isn't designed for large chunks of data. It's really meant for small bits of information, such as boolean preferences etc etc, not to be treated as an easy-to-use database.

Some problems with the user defaults:

  1. The defaults file is read and parsed when you launch your app, regardless of whether you need your information from it at that time or not. This is because other parts of your app also use it for storing their bits of info too.
  2. The entire defaults file needs to be parsed in order for you to retrieve anything, even if you just want a single entry.
  3. You don't choose when the defaults file is parsed. You can't do any smart threading if it becomes huge (say you put 1000 products in there)

I'd recommend either writing the dictionary to it's own plist using NSDictionary's writeToFile: and reading using initWithContentsOfFile: (this still suffers from point #2 above)

OR

Using CoreData/sqlite to write the information to a real database.

NSDictionary methods: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDictionary/writeToFile:atomically:

SomeGuy
  • 9,670
  • 3
  • 32
  • 35
1

An other option (And better in my experience) is to use - NSCoder, this option is great as you can use an Object with normal properties to access your data, which make your code more readable.

You can read about it here - NSCoding / NSKeyed​Archiver by NSHipster

An here is the reference - NSCoder docs

shannoga
  • 19,649
  • 20
  • 104
  • 169
0

NSDictionary has a writeToFile: method which will do it for you.

NSDicationary writeToFile:Atomically:

Theis Egeberg
  • 2,556
  • 21
  • 30
0

use NSUserDefault and save your data like this in array

Here you can use this in anyway in your application for store value of NSUserDefaults.

// --- Saving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];  
// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];

Here you can use this in anyway in your application for get value of NSUserDefaults.

// --- Retrieving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];

Thanks & Cheers ..

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
0

Looks like there can be more amount of data, so the best approach is to use core data to handle this scenario.

You can check few tutorials on how to use core data - Link1 , Link2

There are advantage of using core data over NSUserDefault and file system, as those load all the data at once and you might face some issue in performance.

You can also check following links which will illustrate you performance of different mechanism used to store data - PerformanceLinks1 PerformanceLinks2

Community
  • 1
  • 1
rishi
  • 11,779
  • 4
  • 40
  • 59
0

try this

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:

                                       @"audio.caf",@"pictureAudioKey",
                                       @"audio.m4a",@"englishAudioKey",
                                       @"audio2.m4a",@"spanishAudioKey",
                                       @"audio3.m4a",@"frenchAudioKey",
                                       @"audio4.m4a",@"germanAudioKey",
                                       @"audio5.m4a",@"italianAudioKey",
                                       @"audio6.m4a",@"chineseAudioKey",
                                       @"image.jpg",@"photoimagekey",
                                       @"name.txt", @"identity", 
                                       @"imagename.txt",@"numberkey",nil];

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *dictionaryPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
    dictionaryPath =[dictionaryPath stringByAppendingFormat:@"dicitonary" ] ;
    NSDictionary *savedDictionary = dictionary;


    NSLog(@"The Save file is:%@", savedDictionary);

    [savedDictionary writeToFile:dictionaryPath atomically:YES];
falcon143
  • 702
  • 4
  • 19
  • Don't you think `savedDictionary` will get destroy if u close your app? – Krunal Jul 15 '14 at 07:34
  • use this inside of applicationWillResignActive: then it won't destroy – falcon143 Jul 15 '14 at 07:36
  • Seems a bit redundant creating a how file for something that `NSUserDefaults` does for you. Sorry but I wouldn't do it this way I would use `NSUserDefaults`. – Popeye Jul 15 '14 at 08:16
  • NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults]; [userdefault setObject:@"saveobject" forKey:@"indexpath"]; – falcon143 Jul 15 '14 at 08:18