Search code examples
iosjsonrestkitobjective-c-blocksfoursquare

RestKit 0.10.0 with foursquare API's not retrieving the response when using blocks


Um beginner with RestKit, first example for me was on foursquare API's and I've used RestKit with Blocks not delegates.

I want to retrive the name's of venues,this is the JSON response enter image description here

and this is my code:

// App Delegate.m

    RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString:@"https://api.Foursquare.com/v2"];
RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Venue.sqlite"];
objectManager.objectStore = objectStore;
objectManager.serializationMIMEType = RKMIMETypeJSON;
RKManagedObjectMapping *venueMapping = [RKManagedObjectMapping mappingForClass:[Venue class] inManagedObjectStore:objectStore];

[venueMapping mapKeyPath:@"id" toAttribute:@"id"];
[venueMapping mapKeyPath:@"name" toAttribute:@"name"];
venueMapping.primaryKeyAttribute = @"id";

[objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"response.venue"];

then in myViewController.m

-(void)loadVenues{

// When caling loadObjectsAtResourcePath method it specify RKObjectLoader which is the actual request.
// within these block you can take more options to controll the request.


[[RKObjectManager sharedManager]loadObjectsAtResourcePath:@"/venues/40a55d80f964a52020f31ee3?oauth_token=FNQPN5P5EKLJ5IQ44TMWO00I3W033M0Y1TKINW2OTF2VIOTP&v=20130512" usingBlock:^(RKObjectLoader* loader)
{
    loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Venue class]];
    loader.onDidLoadObject = ^(NSArray *objects)
    {
        NSLog(@"onDidLoadObject Blocks");
        self.data = objects;
        [self.tableView reloadData];
    };
}
 ];

}

and the app is entering the block of onDidLoadObject but every time the array is empty !! even when I test the link on browser it comes with data.

When I debug the loader.URL it always come with these

https://api.Foursquare.com/v2/venues/40a55d80f964a52020f31ee3?v=20130512&oauth_token=FNQPN5P5EKLJ5IQ44TMWO00I3W033M0Y1TKINW2OTF2VIOTP -- https://api.Foursquare.com/v2 -- https://api.Foursquare.com/v2

I don't know why load.URL is wrong ?!

I think um calling the 4square API's with the wrong way, anyone can help ? :)


Solution

  • -Put Your mapping as a class method to be accessed from all application classes, and done only once.

    -change the Attribute "id" as it is reserved in Objective-C.

    -add this to the block

    [loader setObjectMapping:[RestKitMapping yourMapping]];
    

    -then add this with your mapping code

    [objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"response.venue"];
    

    -And use Delegates instead of blocks