insert NSDictionary into CoreData

五迷三道 提交于 2019-11-27 14:03:48
Vincent Bernier

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.

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);
    }
}

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

[managedObject setValuesForKeysWithDictionary:dict];
MGA

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!