0

i am mapping restkit response to core data but it crashes application. I read document of restkit core data mapping even though i am not able to map core data with restkit. And i want to ask few question. I also tried the way use in restkit RKTwitterCoreData. But it also crashes application. But it creates such confusion.

1)Is this necessary to use seed database like used in restkit RKTwitterCoreData example? 2) How can i directly map response to core data class? This is my code.

  RKManagedObjectStore *objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel: [NSManagedObjectModel mergedModelFromBundles:nil]];

[objectStore addSQLitePersistentStoreAtPath: [RKApplicationDataDirectory() stringByAppendingPathComponent:@"CoffeeShop.sqlite"] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
[objectStore createManagedObjectContexts];

RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://api.foursquare.com/v2"]];
[objectManager setManagedObjectStore:objectStore];


RKEntityMapping* venueMapping = [RKEntityMapping mappingForEntityForName:@"Venue" inManagedObjectStore:objectStore];

[venueMapping addAttributeMappingsFromDictionary:@{
 @"name" : @"name",
 @"categories":@"prefix"}];

venueMapping.identificationAttributes=@[@"name"];

RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
pathPattern:nil 
keyPath:@"response.venues"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];

And this is to make request and retrive data.

  RKObjectManager *objectManager=[RKObjectManager sharedManager];
  NSString *latLon = @"23.0280,72.5577";
  NSString *clientID = [NSString stringWithUTF8String:kCLIENTID];
  NSString *clientSecret = [NSString stringWithUTF8String:kCLIENTSECRET];
  NSDictionary *queryParams;
  queryParams = [NSDictionary dictionaryWithObjectsAndKeys:latLon, @"ll",    clientID, @"client_id", clientSecret, @"client_secret", @"coffee", @"query", @"20120602", @"v", nil];

 [objectManager getObjectsAtPath:@"venues/search" parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
     NSLog(@"It Worked: %@",mappingResult.array);
     data=[[NSMutableArray alloc] initWithArray:mappingResult.array];
     NSLog(@"Data Counr%d",[data count]);
     // Or if you're only expecting a single object:
   //  NSLog(@"It Worked: %@", [mappingResult firstObject]);
     //[self.tableView reloadData];

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

And this a crash log

2013-02-05 11:52:19.209 CoffeeShop[2715:11603] I restkit.network:RKHTTPRequestOperation.m:152 GET 'https://api.foursquare.com/v2/venues/search?client_id=U0JM2MAO3202LHDEGSJXOBNJODNWOFK2DS2F0IFDFJYQ2QAT&client_secret=5RNBGWHFWZRXSD4RCESRTIXR22KQSY2IEITAJ52NR1GMUP4F&ll=23.0280,72.5577&query=coffee&v=20120602'
 2013-02-05 11:52:20.863 CoffeeShop[2715:11603] I restkit.network:RKHTTPRequestOperation.m:179 GET 'https://api.foursquare.com/v2/venues/search?client_id=U0JM2MAO3202LHDEGSJXOBNJODNWOFK2DS2F0IFDFJYQ2QAT&client_secret=5RNBGWHFWZRXSD4RCESRTIXR22KQSY2IEITAJ52NR1GMUP4F&ll=23.0280,72.5577&query=coffee&v=20120602' (200 OK) [1.6540 s]
 2013-02-05 11:52:20.880 CoffeeShop[2715:12b03] -[__NSCFArray length]: unrecognized selector sent to instance 0xaa2fd90
 2013-02-05 11:52:20.881 CoffeeShop[2715:12b03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0xaa2fd90'
 *** First throw call stack:(0x1dd1012 0x1bf6e7e 0x1e5c4bd 0x1dc0bbc 0x1dc094e 0x1c6196 0x1c54a2 0x2504d8 0x16a2fe3 0x23af3 0x23fdf 0x251d9 0x2645a 0x2cbff 0x162fd23 0x162fa34 0x17b7a 0x17462 0x1879d 0x19183 0x19a05 0x162fd23 0x162fa34 0x8310e 0x1e4d3f 0x250b014 0x24fad5f 0x24faaa3 0x1e4cba 0x81c91 0x7fc5f 0x162fd23 0x162fa34 0x16bc301 0x24f953f 0x250b014 0x24fc2e8 0x24fbfcb 0x9814fb24 0x981516fe)
 libc++abi.dylib: terminate called throwing an exception

Thanks in advance.

Rajesh Maurya
  • 3,026
  • 4
  • 19
  • 37
  • Seen it many times with RK. The price you pay for 3rd party frameworks. You could do this with `NSHTTPRequest`, `NSJSONSerialization` and Core Data without much more code. – Mundi Feb 05 '13 at 20:41
  • So you are suggesting that i stop to use restkit and have to choose another way. – Rajesh Maurya Feb 06 '13 at 05:21
  • I am suggesting this, yes. But maybe you can find a solution for your problem with the help of the much smaller community that also uses RestKit. – Mundi Feb 06 '13 at 07:26

1 Answers1

0

Your unrecognized selector error is likely masking what would otherwise be an EXC_BAD_ACCESS to a zombie. I bet the underlying cause is the same as what is happening in this question:

RestKit Core Data NSError dealloc Crash

and you can see my answer on how to address that.

Community
  • 1
  • 1
Mike Gottlieb
  • 966
  • 6
  • 11