Mr.KLD
Mr.KLD

Reputation: 2742

RestKit with my JSON data is not working

I'm trying to understand the restkit 0.22. I got some tutorials from different blogs and youtubes. I ended up with mixed up code.

Could anyone please help me with this, I really need it to work for my project.

I created Core Data Model with entities Songs.xcdatamodeld

I have a json that comes from my mySQL db:

[{"SongID":"1","SongTitle":"Song1","PerformerName":"Performer1","SongURL":"http://mysite/mysongs/1.mp3","PerformerPic":"PerfPic1.png"},
{"SongID":"2","SongTitle":"Song2","PerformerName":"Performer2","SongURL":"http://mysite/mysongs/2.mp3","PerformerPic":"PerfPic2.png"},
{"SongID":"3","SongTitle":"Song3","PerformerName":"Performer3","SongURL":"http://mysite/mysongs/3.mp3","PerformerPic":"PerfPic3.png"}]

in AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:http://mysite]];
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Songs" ofType:@"momd"]];

    //Initialize managed object store
    NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL ] mutableCopy];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    objectManager.managedObjectStore = managedObjectStore;
    [RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
    [RKMIMETypeSerialization registeredMIMETypes];
    [objectManager setAcceptHeaderWithMIMEType:@"application/json"];

    RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:@"Songs"
                                                   inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];

    mapping.identificationAttributes = @[@"songID"];

    [mapping addAttributeMappingsFromDictionary:@{@"id" :   @"SongID",
                                                  @"songTitle"  :   @"SongTitle",
                                                  @"performerName"  :   @"PerformerName",
                                                  @"songURL"   :   @"SongURL",
                                                  @"performerPic"   :   @"PerformerPic"}];


    return YES;
}

in TableView Controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

        [RKObjectManager.sharedManager getObjectsAtPath:@"/api.php"
                                         parameters:nil
                                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
     {
         self.Songs = [mappingResult array];

         NSLog(@"It Worked: %@", self.Songs);

     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
         NSLog(@"It Failed: %@", error);
     }];

}

This is the error I'm getting:

GET 'http://mysite/api.php' (200 OK / 0 objects) [request=3.5566s mapping=0.0000s
 total=3.5627s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 
"No response descriptors match the response loaded." UserInfo=0xb5920b0 
{NSErrorFailingURLStringKey=http://mysite/api.php, NSLocalizedFailureReason=A 200
response was loaded from the URL 'http://mysite/api.php', which failed to match all (0)
 response descriptors:, NSLocalizedDescription=No response descriptors match the response
 loaded., keyPath=null, NSErrorFailingURLKey=http://mysite/api.php, 
NSUnderlyingError=0xb5921b0 "No mappable object representations were found at the key
 paths searched."}

Upvotes: 0

Views: 193

Answers (1)

Wain
Wain

Reputation: 119031

First, you don't seem to be creating the managed object contexts as part of your setup code. This will likely cause you issues after you fix your 'main' issue:

Your main issue is quite clearly described in the error message:

which failed to match all (0) response descriptors

I.e. You haven't created any response descriptors.

The Object-mapping guide walks you through the mapping and descriptor creation process (and includes lots more details to boot).


Start with something like:

RKResponseDescriptor *rd = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

then add the mapping to the object manager.

(ensure you're calling createManagedObjectContexts somewhere).

Upvotes: 1

Related Questions