0

I have a JSON returned by REST API to my already existing app that I am trying to fix. I am fairly new to objective C.

[
{
"Activities":"
         [
           {
             "activityid":845,
             "activityname":"Registration and networking breakfast",
             "actvitydesc":"Registration and networking breakfast",

           },
           {
             "activityid":846,
             "activityname":"Plenary session: The Workforce Tsunami",
             "actvitydesc":"It's Time to Rethink Talent 

           }
}
]

There is a core data entity Activity in my app, which contains the following attributes Activityid activityname activitydesc

How can I insert the JSON data inside my core data entity? Is there any need to create model class to do that? Can I insert my json data directly into core data without creating model objects?

2
  • Did you solve the problem? Commented Jan 9, 2017 at 8:37
  • The code that you gave had to be changed a little bit, but it worked. Thanks Commented Jan 9, 2017 at 14:10

2 Answers 2

1

If you already have the entity called Activity you can use the NSManagedObject class to set the value for an Attribute. Try this

NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Activity" inManagedObjectContext:_managedObjectContext];

[managedObject setValue:[NSNumber numberWithInteger:4711] forKey:@"activityid"];

and so on... You can also create the class by the Classgenerator of CoreData in XCode 8 there are multiple ways. Defaultly the class is generated automatically since XCode 8. If you don't like this you can disable it and generate the class manually. Just go to the CoreData Model --> Editor --> Create NSManagedObject Subclass. Note you have to deactivate the automatic code generation before. If you don't do that, you will become errors while building the project. If the name of the JSON Attribute is equal to the name of the CoreData Attribute you can also loop over the Dictionary like this:

NSArray *wrapper = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
for(NSDictionary *dict in wrapper){
    NSArray *activities = [dict objectForKey:@"Activities"];
    for(NSDictionary *activity in activities){
        NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Activity" inManagedObjectContext:_managedObjectContext];

       // [managedObject setValue:[NSNumber numberWithInteger:4711] forKey:@"activityid"];
        for(NSString *attributeName in activity)
            [managedObject setValue:[activity objectForKey:attributeName] forKey:attributeName];

    }
}

Hope that helps you...

Sign up to request clarification or add additional context in comments.

3 Comments

But the question is how can I store the array of activities inside Activity entity.? How do I do that? Can I do that?
The Question is why do you want to store that array inside the activity entity? The way I showed you, creates a new entry in the activity table for you. So every entry in the jsonarray becomes a own entry in the activity table of core Data. If you want to store arrays in a core data entity you have to choose transformable as attribute type of your core data attribute. But I think this is not what you want.
Was my answer helpful for you?
0

To insert anything to core data you need to generate models. Create core data model editor (if you don't have one) and add Entities. There is lots of tutorials on the web how to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.