我正在将 restkit 响应映射到核心数据,但它会使应用程序崩溃。即使我无法使用restkit映射核心数据,我也阅读了restkit核心数据映射的文档。我想问几个问题。我还尝试了在 restkit RKTwitterCoreData中使用的方式。但它也会使应用程序崩溃。但它会造成这样的混乱。
1)这是否需要使用restkit RKTwitterCoreData示例中使用的种子数据库?2)如何直接将响应映射到核心数据类?这是我的代码。
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];
这是发出请求和检索数据。
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);
}];
这是一个崩溃日志
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
提前致谢。