0

I am trying to store data fetched API which is in form of array of dictionaries. I want to store these values in my database. API response stores in an object.

[
  {
    "sort_priority": 1,
    "desc": "",
    "img_url": "",
    "id": 3,
    "name": "First Run"
  },
  {
    "sort_priority": 2,
    "desc": "",
    "img_url": "",
    "id": 4,
    "name": "Shorts"
  },
  {
    "sort_priority": 3,
    "desc": "",
    "img_url": "",
    "id": 1,
    "name": "Animation"
  },
  {
    "sort_priority": 4,
    "desc": "",
    "img_url": "",
    "id": 2,
    "name": "Documentary"
  }
]

I just want to store name and id from this list.

[Categories MR_importFromObject:responseObject];

Categories is an Entity and responseObject is an object returned from API

General Failure
  • 2,421
  • 4
  • 23
  • 49
  • Are you trying to store record in coreData? If yes, then create an object within Entity of type 'Transformable'. Generate NSManagedObject subclass & replace object type to array. – Gagan_iOS Jun 08 '16 at 07:44
  • could you please give me an example? if I run `[Categories MR_importFromObject:responseObject];` it stores data in `name` as `( First Run, Shorts, Animation, Documentary)` – Ahmad Nauman Jun 08 '16 at 07:50
  • http://stackoverflow.com/questions/25416132/how-do-you-store-data-from-nsmutable-array-in-core-data - best example ever. – Himanshu padia Jun 08 '16 at 07:51
  • Yes, Himanshu has done the job. You can also search for the same e.g. http://lextech.com/2013/01/core-data-transformable-attributes/ – Gagan_iOS Jun 08 '16 at 07:54
  • Thanks @himanshupadia and @Gagan_iOS for help. if `name` stores `( First Run, Shorts, Animation, Documentary)`. So how to get each category name in single tableviewcell using indexpath? – Ahmad Nauman Jun 08 '16 at 07:58
  • You can store core data in array and fetch it and display in tableview. – Himanshu padia Jun 08 '16 at 08:50
  • I have to store this data in database and then have fetch it from there. – Ahmad Nauman Jun 08 '16 at 09:13

2 Answers2

0
for (int i=0; i<resultArr.count; i++)
    {
            NSError *error=nil;
            NSManagedObjectContext *context=[self managedObjectContext];
            NSManagedObject *newDevice=[NSEntityDescription insertNewObjectForEntityForName:@"myEntity" inManagedObjectContext:context];


            [newDevice setValue:[[resultArr objectAtIndex:i ]valueForKey:@"name"] forKey:@"name"] ;
            [newDevice setValue:[[resultArr objectAtIndex:i ]valueForKey:@"id"]  forKey:@"id"];


            [newDevice.managedObjectContext save:&error]) 


}

  //For fetchig/////////////////////

  NSError *error;
_dataEntity = [NSEntityDescription entityForName:@"myEntity" inManagedObjectContext:appDelegate.managedObjectContext];
NSFetchRequest * fr = [[NSFetchRequest alloc]init];
[fr setEntity:_dataEntity];
NSMutableArray *namesArr=[[NSMutableArray alloc]init];
NSMutableArray * result = [[appDelegate.managedObjectContext executeFetchRequest:fr error:&error] mutableCopy];
for (unsigned short int i=0;i<result.count;i++)
{
    NSMutableDictionary *coreDataDict=[[NSMutableDictionary alloc] init];
    NSManagedObject *fetchResult=[result  objectAtIndex:i];

    [coreDataDict setValue:[fetchResult valueForKey:@"name"] forKey:@"name"];


    [namesArr addObject:coreDataDict];

}
Dinesh Gurrapu
  • 158
  • 1
  • 1
  • 11
  • dear @Dinesh when I fetch back data from database its count always be one. when I call `[_movieCategories objectAtIndex:indexPath.row-1]name]`. it returns 1 and `name` contains data something like that `( First Run, Shorts, Animation, Documentary) ` – Ahmad Nauman Jun 08 '16 at 08:45
  • 1st u can add it to dictionary then add that dictionary to the array, finally u can return the array count – Dinesh Gurrapu Jun 08 '16 at 08:53
  • I am storing data using magical record `[Categories MR_importFromObject:responseObject]; ` and fetching back that data having attribute `name ` from database ` NSArray* movieCategories = [Categories MR_findAll]; ` but when i print count value of this array it returns one `NSLog(@"Category count: %d",movieCategories.count);` – Ahmad Nauman Jun 08 '16 at 09:12
  • thanks dear it solves my problem `NSArray *responseArray = (NSArray *)responseObject;[Categories MR_importFromArray:responseArray];` – Ahmad Nauman Jun 10 '16 at 10:20
0

thanks to all my problem has solved by doing this

NSArray *responseArray = (NSArray *)responseObject;
[Categories MR_importFromArray:responseArray];

I had a array of multiple dictionarys in responseObject and want to store only specific values from each dictionary. So I did above mention and it stored positives in database.