我正在将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
提前致谢。
答案 0 :(得分:0)
您无法识别的选择器错误可能会掩盖对僵尸的EXC_BAD_ACCESS。我打赌潜在的原因与这个问题中发生的事情是一样的:
RestKit Core Data NSError dealloc Crash
你可以看到我如何解决这个问题的答案。