iosjsoncore-datamagicalrecord

Converting and storing JSON data into iOS Core Data


Basically I'm working on an app to allow users to track their tv shows. A user can click their tv show to get a season and episode breakdown.

To achieve this, I am trying to gather JSON data from this API, and then store the data into core data. The API call is this: http://api.trakt.tv/show/summary.json/36590b30dc7d0db9ebd3153b1a989e5d/arrow/1

I can successfully store the values of: title, year, url, first_aired etc. But I can't work out how to store the season and episode information into my core data (located about half way down the JSON API call)

I have included a link to a screenshot of how I've set out my core data model: !http://i546.photobucket.com/albums/hh427/camcham/ScreenShot2013-10-17at34449AM.png

The code below is how I'm currently trying to store the JSON data into my core data (using MagicalRecords)

NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];


        Show *showp = [Show MR_findFirstByAttribute:@"sID" withValue:showID inContext:localContext];
        if (![showp.sID isEqualToString:showID])
        {
            //Create New Show in current thread
            Show *showContext = [Show MR_createInContext:localContext];
            showContext.title = showTitle;
            showContext.poster = showPoster;
            showContext.year = showYear;
            showContext.sID = showID;
            //code above this comment correctly adds right JSON info to core data and i can access and display it properly

The next part of my code I have tried to convert an NSArray to NSSet, as my 'seasons' relationship is of type NSSet, however I believe the JSON data is NSArray. I am getting the following error: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber managedObjectContext]: unrecognized selector sent to instance 0xa22a680'

                NSArray *show = [(NSSet *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
            showContext.seasons = [NSSet setWithArray:show];

The code below does not work as intended. episode.title for example, stores every single episode's title, instead of just the single title for a particular episode.

            Season *season = [Season MR_createInContext:localContext];
            season.seasonNumber = [(NSDictionary *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
            season.episodes = [(NSDictionary *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"episodes"];


            Episode *episode = [Episode MR_createInContext:localContext];
            episode.title = [[(NSDictionary *)[season.episodes objectForKey:@"seasons"] valueForKey:@"episodes"] valueForKey:@"title"];
            episode.overview = [[(NSDictionary *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"episodes"] valueForKey:@"overview"];

So to sum it all up, I would love for someone to demonstrate the correct way to store the tv seasons and episodes from my JSON API, so I can then utilise this data in my app!


Solution

  • Removing the cast to (NSSet *) should fix the error.

    NSArray *show = [[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
    showContext.seasons = [NSSet setWithArray:show];
    

    instead of

    NSArray *show = [(NSSet *)[JSONEvents objectForKey:@"seasons"] valueForKey:@"season"];
    showContext.seasons = [NSSet setWithArray:show];