RestKit:被调用postObject代表什么方法?(RestKit: What delegat

2019-06-23 22:37发布

用什么方法RKObjectLoaderDelegate的获取方法之后调用- (RKObjectLoader *)postObject:(id<NSObject>)object delegate:(id<RKObjectLoaderDelegate>)delegate被称为?

我想一旦我的POST成功运行更多的代码。

编辑(详细信息):

无论是那@保罗提到的方法被调用,但我的服务器在某种程度上正确地解释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];

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

错误域= JKErrorDomain代码= -1“意外的道理,想 '{', '}', '[', ']',”, ' ':', '真', '假', '空',' “STRING””, '号'。” 的UserInfo = 0x7b9f510 {JKAtIndexKey = 0,JKLineNumberKey = 1,NSLocalizedDescription =意外标记,想 '{', '}', '[', ']', '', ':', '真', '假', '空', ' “STRING”', '号'。}

Answer 1:

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

要么

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


Answer 2:

一旦你设置你的正确的映射对象的发布是很容易的。 像塞尚指出,相应的回调将被使用。

对于发布对象,但是,你还需要一个序列化映射,告诉哪些参数应转换为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);


Answer 3:

确保RKObjectManager已初始化:

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

我浪费了大量的时间注意到,sharedManager是不是一个真正的单之前,你必须初始化它。

之后,你可以使用sharedManager并获得创建的第一个参考的:

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


文章来源: RestKit: What delegate method is called for postObject?
标签: ios restkit