使用RKObjectManager从IOS POST进行INSERT

时间:2012-07-31 18:05:29

标签: objective-c ios restkit

我使用Restkit的RKObjectManager和RKObjectMapping使用GET从Web服务成功检索JSON数据。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://192.168.1.12"];
    RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
    objectManager.client.baseURL = baseURL;

    RKObjectMapping *markerMapping = [RKObjectMapping mappingForClass:[Marker class]];
    [markerMapping mapKeyPath:@"la" toAttribute:@"lat"];
    [markerMapping mapKeyPath:@"lo" toAttribute:@"lon"];
    [markerMapping mapKeyPath:@"des" toAttribute:@"description"];
    [objectManager.mappingProvider setMapping:markerMapping forKeyPath:@"markers"];
}

- (void)sendGet
{
    NSString *lat = @"53.334932";
    NSString *lon = @"-0.543749";
    NSString *dist = @"1000";

    NSDictionary *queryParams;
    queryParams = [NSDictionary dictionaryWithObjectsAndKeys:lat, @"la", lon, @"lo", dist, @"d", nil];
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:@"/get_markers.php" queryParameters:queryParams];
    [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self];
}

上面的代码正在运行,我很高兴使用返回的数据。但是,我无法POST一条记录要插入数据库中。我想我可以重用sharedManager甚至可能重用映射。我不需要处理任何回报。

我尝试了以下代码段的多种变体形式。无济于事。

第一个片段是尝试重用管理器和映射。忽略这个伪代码中的映射范围明显的缺陷是我无法弄清楚如何POST到正确的URL资源(baseURL&'/ insert_marker.php')。所以我实际上没有尝试过这个:

- (void)postInsert
{  
    Marker *mkr = [Marker alloc];
    mkr.lat = @"53.334933";
    mkr.lon = @"-0.543750";
    mkr.description = @"some words";

    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    [objectManager.mappingProvider setSerializationMapping:[markerMapping inverseMapping] forClass:[Marker class]];
    [[RKObjectManager objectManager] postObject:mkr delegate:nil];
}

此第二个代码段运行但崩溃了应用程序。显然,字符串和url类型之间存在混淆,但对象管理器的帖子不会使用url进行编译。我很困惑!

- (void)postInsert
{  
    RKParams* params = [RKParams params];
    [params setValue:@"53.334933" forParam:@"la"];
    [params setValue:@"-0.543750" forParam:@"l0"];
    [params setValue:@"some words" forParam:@"des"];

    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    NSString *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:@"/add_marker.php"];
    [objectManager.client post:URL params:params delegate:nil];
}

那么,如何使用params进行POST(希望重用sharedManager,可能是映射而不是处理返回)?

1 个答案:

答案 0 :(得分:0)

如评论中所述,请尝试使用NSStrings而不是RKURL:

NSString *baseUrl = @"https://mysite.com"; 
[RKObjectManager objectManagerWithBaseURLString:baseUrl];

如果你想做一些更健壮的东西,你也可以尝试使用objectManager而不是objectManager上的客户端发布帖子,例如你可以使用块:

- (void) post: (id) myEntity onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock showingLoader:(BOOL)loaderWillShow{

    RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[[myEntity class] class]];
    [[RKObjectManager sharedManager] postObject:myEntity usingBlock:^(RKObjectLoader *loader) {
        [self settingsForLoader:loader withMapping:mapping onLoad:loadBlock onError:failBlock showingLoader:loaderWillShow];
    }];

}

//Settings For Loader definition:



- (void) settingsForLoader: (RKObjectLoader *) loader withMapping: (RKObjectMapping *) mapping onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock showingLoader:(BOOL) loaderWillShow{

    loader.objectMapping = mapping;
    loader.delegate = self;
    loader.onDidLoadObject = loadBlock;

    loader.onDidFailWithError = ^(NSError * error){
       //NSLog(@"%@",error);
    };

    loader.onDidFailLoadWithError = failBlock;
    loader.onDidLoadResponse = ^(RKResponse *response) {
        [self fireErrorBlock:failBlock onErrorInResponse:response disablingLoader:loaderWillShow];
    };

}