RestKit:为postObject调用什么委托方法?

时间:2012-04-25 00:01:43

标签: ios restkit

调用方法- (RKObjectLoader *)postObject:(id<NSObject>)object delegate:(id<RKObjectLoaderDelegate>)delegate后调用RKObjectLoaderDelegate的方法是什么?

我的POST成功后,我正在尝试运行更多代码。

编辑(更多信息):

@Paul提及的方法都没有被调用,但是我的服务器正在正确地解释POST,因为正在写入DB。

在应用初始化期间,我正在执行以下操作:

// Grab the reference to the router from the manager
RKObjectRouter *router = [RKObjectManager sharedManager].router;

// Define a default resource path for all unspecified HTTP verbs
[router routeClass:[EventMessage class] toResourcePath:@"/events_messages/:idPrimaryKey"];
[router routeClass:[EventMessage class] toResourcePath:@"/event_messages" forMethod:RKRequestMethodPOST];

当我准备发帖时,我这样做:

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[EventMessage class]];
[objectMapping mapKeyPath:@"user_id" toAttribute:@"userId"];
[objectMapping mapKeyPath:@"event_id" toAttribute:@"eventId"];
[objectMapping mapKeyPath:@"message" toAttribute:@"message"];

[[RKObjectManager sharedManager].mappingProvider registerMapping:objectMapping withRootKeyPath:@"event_message"];

[[RKObjectManager sharedManager] postObject:eventMessage delegate:self];

这是我在控制台中遇到的错误:

  

错误Domain = JKErrorDomain Code = -1“意外令牌,想要'{','}','[',']',',',':','true','false','null ',''STRING'','NUMBER'。“ UserInfo = 0x7b9f510 {JKAtIndexKey = 0,JKLineNumberKey = 1,NSLocalizedDescription =意外的令牌,想要'{','}','[',']',',',':','true','false', 'null','“STRING”','NUMBER'。}

3 个答案:

答案 0 :(得分:2)

    - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;

    - (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;

答案 1 :(得分:2)

正确设置映射后,对象的发布非常简单。就像Paul Cezanne指出的那样,将使用相应的回调。

但是,对于发布对象,您还需要一个序列化映射,告诉框架哪些参数应转换为POST参数:

映射示例:

    // Serialisation Mapping
    RKObjectMapping *messageSendMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];

    [messageSendMapping mapAttributes:@"name", @"number", @"text", nil];

    [[RKObjectManager sharedManager].mappingProvider
            setSerializationMapping:messageSendMapping
                           forClass:[Message class]];

更新:出于问题排查的目的,您可能希望增加RestKit的日志级别,如下所示:

RKLogConfigureByName("RestKit", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace);

答案 2 :(得分:0)

确保已初始化RKObjectManager:

NSString *url = @"http://example.com";
RKObjectManager* mgr = [RKObjectManager managerWithBaseURLString:url];

我浪费了很多时间才注意到sharedManager不是真正的单例,你必须初始化它。

之后,您可以使用sharedManager并获取对创建的第一个的引用:

[[RKObjectManager sharedManager].mappingProvider setObjectMapping:mapping forResourcePathPattern:@"/RKKeyValueMappingExample"];