目前,我正在使用使用RestKit进行通信的iOS应用程序。 我成功检索并添加了数据。但是,更新和删除数据库中的数据无效。
我正在使用以下代码向数据库添加新条目:
[[RKClient sharedClient] get:@"/tblTests" delegate:self];
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://10.10.3.101/WcfService1/WcfDataService1.svc/"];
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager.client.baseURL = baseURL;
[venueMapping mapKeyPathsToAttributes:@"Name",@"name", nil];
[objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"value"];
[objectManager loadObjectsAtResourcePath:@"/tblTests/" delegate:self];
RKObjectMapping* venueMappingS = [RKObjectMapping mappingForClass:[Venue class]];
venueMappingS.setDefaultValueForMissingAttributes = YES;
[venueMappingS mapKeyPath:@"Name" toAttribute:@"name"];
RKObjectRouter *router = [[RKObjectRouter alloc] init];
[router routeClass:[Venue class] toResourcePath:@"/tblTests/"];
[router routeClass:[Venue class] toResourcePath:@"/tblTests/" forMethod:RKRequestMethodPOST];
[[[RKObjectManager sharedManager] mappingProvider] setMapping:venueMappingS forKeyPath:@"value"];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[venueMappingS inverseMapping] forClass:[Venue class]];
[RKObjectManager sharedManager].router = router;
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON;
Venue *venueName = [[Venue alloc] init];
venueName.name = @"Midhun MP";
venueName.Id = 12;
[[RKObjectManager sharedManager] postObject:venueName delegate:self];
它工作得很好,然后我使用相同的代码只更改这两行:
[router routeClass:[Venue class] toResourcePath:@"/tblTests/" forMethod:RKRequestMethodPOST];
[[RKObjectManager sharedManager] postObject:venueName delegate:self];
到
[router routeClass:[Venue class] toResourcePath:@"/tblTests/" forMethod:RKRequestMethodDELETE];
[[RKObjectManager sharedManager] deleteObject:venueName delegate:self];
这不行!!!
我的场地课程:
@interface Venue : NSObject
@property (strong, nonatomic) NSString *name;
@property (nonatomic) int Id;
@end
我的JSON看起来像:
{"value":[{"Id":1,"Name":"Midhun"},{"Id":12,"Name":"Midhun MP"}]}
我找不到任何问题,请帮助我。
答案 0 :(得分:1)
当您更新/删除对象并使用restful Web服务时,您需要告知应该更新哪个对象(带有它的ID)。在RestKit中,您可以通过告诉路由器使用给定的:attribute
来实现此目的。
[router routeClass:[Venue class] toResourcePath:@"/tblTests/:Id" forMethod:RKRequestMethodPOST];
BTW好像你使用的RestKit 0.10很快就会过时。更新到0.20并找到一些更容易的方法和块的使用将更容易。