0

目前我正在使用一个使用 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"}]}

我找不到任何问题,请帮助我。

4

1 回答 1

1

当您更新/删除一个对象并使用一个安静的网络服务时,您需要告诉哪个对象(带有它的 ID)应该被更新。在 RestKit 中,您可以通过告诉路由器使用给定的:attribute.

[router routeClass:[Venue class] toResourcePath:@"/tblTests/:Id" forMethod:RKRequestMethodPOST];

顺便说一句,您使用的 RestKit 0.10 似乎很快就会过时。更新到 0.20 并找到一些更简单的方法和块的使用会容易得多。

于 2013-04-08T17:22:15.917 回答