Keith
Keith

Reputation: 1372

NSJSONSerialization to Core Data

I'm having a programming getting some json data into core data. I can get the entity populated, the problem comes in when I'm trying to populate the relationship's entity.

I've tried many different things but this is what I have right now:

NSArray *jsonArray = [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:NSJSONReadingMutableContainers
                      error:&error];

Group *groupModel = [NSEntityDescription
                     insertNewObjectForEntityForName:@"Group"
                     inManagedObjectContext:context];

//NSLog(@"%@", jsonArray);

for (NSDictionary *array in jsonArray)
{
    groupModel.name = [array valueForKey:@"name"];

    NSSet *itemSet = [array valueForKey:@"items"];
    NSLog(@"%@", itemSet);
    groupModel.deck = itemSet;

    for (NSDictionary *item in [array valueForKey:@"items"])
    {
        //groupModel.deck = telephoneSet;
    }

    //NSLog(@"%@", [array valueForKey:@"name"]);

    //NSLog(@"%@", [array valueForKey:@"items"]);
}

I've tried all sorts of things to get this data inserted and I can't figure it out. I know that my class Group has a relationship with Deck:

@property (nonatomic, retain) NSSet *deck;

So I'm trying to pass an NSSet converted from json but I can't exactly figure it out. I've tried looping through it all sorts of different ways and converting it to different types.

[
    {
        "name": "Default",
        "items": [
            {
                "name": "Name 1",
                "weapon": "Weapon 1",
                "type": "Type 1"
            },
            {
                "name": "Name 2",
                "weapon": "Weapon 2",
                "type": "Type 2"
            }
        ]
    }
]

The Items from the array need to be converted into a set and passed to the group property. That is the part I can't figure out.

Deck.h:

@interface Deck : NSManagedObject

@property (nonatomic, retain) NSString * info;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * weapon;
@property (nonatomic, retain) Group *group;

Group.h

@interface Group : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *deck;
@end

@interface Group (CoreDataGeneratedAccessors)

- (void)addDeckObject:(Deck *)value;
- (void)removeDeckObject:(Deck *)value;
- (void)addDeck:(NSSet *)values;
- (void)removeDeck:(NSSet *)values;

Upvotes: 1

Views: 264

Answers (1)

Wain
Wain

Reputation: 119041

I'd encourage you to use RestKit to help with this.

You have one main fundamental issue:

  1. You never create any instances of Deck. You need to. You're trying to set an array of dictionaries to deck which expects a set of Deck objects.

Basically, in your for (NSDictionary *item ... loop the first thing you should be doing is creating a Deck instance. Then configure it using the item dictionary contents. Then call:

[groupModel addDeckObject:deck];

(don't bother trying to set an NSSet with all the Deck objects are you need to iterate anyway).

Upvotes: 1

Related Questions