RESTKit:两个终点POST,PUT,DELETE
我有两个EndPoints:
http://rest.domain.com/invite
http://rest.domain.com/template
根据用户select的选项,我需要能够在两个EndPoint上进行PUT
, POST
和Delete
。
所有三种方法的映射略有不同。 每个EndPoint的映射也不同。
什么是设置映射的最佳方法,以便它们被加载一次,并且可以针对每个EndPoint使用多次,具体取决于用户select的选项( PUT
, POST
或Delete
)? 我必须在一个故事板上完成这个场景!
目前,下面是我在POST
/invite
EndPoint时使用的代码(在重映射后的第一个POST b / c之后崩溃):
- (void)sendInvite:(NSInteger)methodType { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; self.objectManager = [self getObjectManager]; self.objectManager.managedObjectStore = appDelegate.managedObjectStore; RKEntityMapping *invitationMapping = [RKEntityMapping mappingForEntityForName:kInvite inManagedObjectStore:self.objectManager.managedObjectStore]; RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:kActivity inManagedObjectStore:self.objectManager.managedObjectStore]; Invite *invitation; switch (methodType) { case POST: { invitationMapping = [RESTMappingProvider invitionPostMapping:invitationMapping]; activityMapping = [RESTMappingProvider activityPostMapping:activityMapping]; [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kActivitiesRelationship toKeyPath:kActivitiesRelationship withMapping:activityMapping]]; invitation = [self inviteForMethod:POST]; //This method just assigns values to the attributes [self setupDescriptors:invitationMapping forKeyPath:kMeetupKeyPath descriptorClassIsTemplate:NO]; [self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [self.objectManager.managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:NO]; [self.objectManager postObject:invitation path:kMeetupKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { [self removeDuplicateObjects]; //After removing relationship Dups, I save to persistent store } failure:^(RKObjectRequestOperation *operation, NSError *error) { }]; } break; case PUTACCEPT: case PUTDECLINE: case PUTEDIT: { invitationMapping = [RESTMappingProvider invitionPutMapping:invitationMapping]; activityMapping = [RESTMappingProvider activityPutMapping:activityMapping]; [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kActivitiesRelationship toKeyPath:kActivitiesRelationship withMapping:activityMapping]]; invitation = [self inviteForMethod:methodType]; //This method just assigns values to the attributes [self setupDescriptors:invitationMapping forKeyPath:kMeetupKeyPath descriptorClassIsTemplate:NO]; [self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [self.objectManager putObject:invitation path:kMeetupKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Failure in PUT"); }]; } break; } }
我为模板EndPoint做类似的事情
- (void)saveAsTemplate { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; self.objectManager = [self getObjectManager]; self.objectManager.managedObjectStore = appDelegate.managedObjectStore; RKEntityMapping *invitationMapping = [RKEntityMapping mappingForEntityForName:kInviteTemplates inManagedObjectStore:self.objectManager.managedObjectStore]; invitationMapping = [RESTMappingProvider invitionTemplateMapping:invitationMapping]; RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:kActivityTemplates inManagedObjectStore:self.objectManager.managedObjectStore]; activityMapping = [RESTMappingProvider activityTemplateMapping:activityMapping]; [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kTemplateActivitiesRelationship toKeyPath:kTemplateActivitiesRelationship withMapping:activityMapping]]; STInvitesTemplate *invitation = [self templateForInvite];//this method assigns values to the attributes [self setupDescriptors:invitationMapping forKeyPath:kTemplatesKeyPath descriptorClassIsTemplate:YES]; [self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [self.objectManager postObject:invitation path:kTemplatesKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { } failure:^(RKObjectRequestOperation *operation, NSError *error) { }]; }
请求和响应描述符:
- (void)setupDescriptors:(RKEntityMapping *)invitationMapping forKeyPath:(NSString *)keyPath descriptorClassIsTemplate:(BOOL)isTemplate { RKRequestDescriptor *requestDescriptor; if (isTemplate) { requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Template class] rootKeyPath:nil method:RKRequestMethodAny]; } else { requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Invite class] rootKeyPath:nil method:RKRequestMethodAny]; } NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:invitationMapping method:RKRequestMethodGET pathPattern:keyPath keyPath:nil statusCodes:statusCodeSet]; self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON; [self.objectManager addRequestDescriptor:requestDescriptor]; [self.objectManager addResponseDescriptor:responseDescriptor]; }
我知道上面的方法是不正确的,因为Xcode在POST或PUT后崩溃。 我还没有实现删除但B / C我不知道如何正确设置。
我在viewDidLoad
加载映射吗? 我是否创buildPUT, POST, DELETE
x 2 EndPoints
= 6 RKEntityMappings
?
需要一些最佳实践的指导。 代码示例或一些分步说明将是伟大的!
您需要创build许多不同的映射,因为您对stream程有不同的结构响应。 如果一个实体types的响应是一些公共超集的子集,那么只能使用一个(用于映射响应,另一个用于请求)。 你不会说预期的JSON,所以我不能告诉。
在你的代码中,我看到2个请求描述符和1个响应描述符。 请求与任何方法匹配,所以将始终使用。 响应描述符只匹配GET响应,因此不适用于任何事情。 每个方法的每个端点可能有一个响应描述符(正如你所说的,他们需要应用不同的映射)。
viewDidLoad
不一定是正确的地方来configuration这个。 它看起来像你有一个单一的对象pipe理器,这个configuration代码应该在创build时运行(不是在使用时,因为视图可以被加载多次,configuration将被复制)。