Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert NSDictionary into CoreData

I have an NSDictionary and a CoreData Database. I want to insert the NSDictionary into the database.

  1. How can I do this (code snippets if possible)?

  2. What is the suitable type for the attribute of the dictionary ?

like image 759
OXXY Avatar asked Dec 30 '11 17:12

OXXY


4 Answers

You would need to serialize your NSDictionary to an NSData, CoreData can hold to NSData.
But you won't be able to search (predicate) element of your NSDictionary.

And if we think about this, NSDictionary is a collection of data.
A Table in a database is kind of a collection of data.
In CoreData the closest thing you got to a collection of data is NSManagedObject.

So my advice would be to make a NSManagedObject subclass that would hold the information you have in your NSDictionary. The key would be the attribute and the value would be the value of that attribute. And you would be able to search based on the attribute of that NSManagedObject subclass.

like image 98
Vincent Bernier Avatar answered Oct 10 '22 15:10

Vincent Bernier


I found another way to add Dictionary into Coredata by creating an attribute with data type 'Transformable'.

For example create an entity in your project & attribute with data type Transformable. Generate subclass for NSManagedObject. Attribute will be available with data type 'id', change into NSDictionary.

Below is what I did (My NSManagedObject sub class name is 'DictTest')

-(void)InsertIntoDataBase
{
    DictTest *entityDict=(DictTest*)[NSEntityDescription insertNewObjectForEntityForName:@"DictTest" inManagedObjectContext:self.managedObjectContext];
    NSMutableDictionary *mutDict=[NSMutableDictionary dictionary];
    [mutDict setValue:@"1" forKey:@"1"];
    [mutDict setValue:@"2" forKey:@"2"];
    [mutDict setValue:@"3" forKey:@"3"];
    [mutDict setValue:@"4" forKey:@"4"];
    [mutDict setValue:@"5" forKey:@"5"];
    [mutDict setValue:@"6" forKey:@"6"];
    [mutDict setValue:@"7" forKey:@"7"];
    [mutDict setValue:@"8" forKey:@"8"];
    [mutDict setValue:@"9" forKey:@"9"];
    [mutDict setValue:@"10" forKey:@"10"];
    [entityDict setInfoDict:mutDict];
    NSError *error;
    if(![self.managedObjectContext save:&error])
    {
        NSLog(@"error description is : %@",error.localizedDescription);
    }
    else{
        NSLog(@"Saved");

    }
}

for Fetching records

-(void)FetchRecord
{
    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"DictTest" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
     NSArray *fetchArray=  [self.managedObjectContext executeFetchRequest:request error:nil];
    for (DictTest *obj in fetchArray) {
        NSLog(@"Dict is : %@",obj.infoDict);
    }
}
like image 25
Gagan_iOS Avatar answered Oct 10 '22 15:10

Gagan_iOS


Set up your entity description, insert a new object, then use:

[managedObject setValuesForKeysWithDictionary:dict];
like image 42
malhal Avatar answered Oct 10 '22 15:10

malhal


Why don't you just create an entity with all the data from your NSDictionary and then just parse through it.

Check this out for some CoreData code snippets. You can simply create a few entities to store the info of your dictionaries. You can then parse through your dictionary and save the proper attributes:

 NSManagedObject *photoObject = [NSEntityDescription insertNewObjectForEntityForName:@"Photo"
                                                              inManagedObjectContext:context];    
 [photoObject setPhotographer:[myDictionary objectForKey:@"photographer"]];
 and so on...

No matter how complex you XML data structure, if you can set up a nice entity structure, it is fairly easy to simply dumb it all in CoreData. You'll also have a much easier time with queries if you take the time to create entities instead of just dumping the whole dictionary in a single field.

like image 35
MGA Avatar answered Oct 10 '22 15:10

MGA