PUT 操作响应未保存在核心数据存储中



我正在尝试使用 RestKit 使用 github 对用户进行身份验证,并将响应保存回核心数据以挂在 oauth 令牌上。响应返回 200 或 201 很好,但没有任何内容被保存回上下文。我假设我缺少一些小东西,但我无法弄清楚。这是怎么回事:

这发生在视图中DidLoad

RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"SLAuthentication" inManagedObjectStore:managedObjectStore];
NSString *clientID = [(SLAppDelegate *)[[UIApplication sharedApplication] delegate] clientID];

[entityMapping addAttributeMappingsFromDictionary:@{@"id" : @"tokenID", @"url" : @"url", @"token" : @"oauthToken", @"note" : @"note"}];
entityMapping.identificationAttributes = @[@"tokenID"];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodPUT pathPattern:[NSString stringWithFormat:@"/authorizations/clients/%@", clientID] keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];

这是发出请求的地方

NSManagedObjectContext *context = [[[RKObjectManager sharedManager] managedObjectStore] mainQueueManagedObjectContext];
NSString *secret = [(SLAppDelegate *)[[UIApplication sharedApplication] delegate] clientSecret];
NSString *clientID = [(SLAppDelegate *) [[UIApplication sharedApplication] delegate] clientID];
[[RKObjectManager sharedManager].HTTPClient setAuthorizationHeaderWithUsername:userName password:pass];
[[RKObjectManager sharedManager] setRequestSerializationMIMEType:@"application/json"];

[[RKObjectManager sharedManager] putObject:nil path :[NSString stringWithFormat:@"/authorizations/clients/%@", clientID] parameters:@{@"client_secret": secret, @"scopes" : @[@"repo"]} success:nil failure:nil];

//Flush the username and password so that we aren't storing them anymore.
[[RKObjectManager sharedManager].HTTPClient clearAuthorizationHeader];
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"SLUser"];
NSError *error = nil;
NSArray *users = [context executeFetchRequest:request error:&error];
[[users firstObject] setUserName:userName];
[context save:&error];

但什么也没回来。API 的说明如下:http://developer.github.com/v3/oauth/#get-or-create-an-authorization-for-a-specific-app

响应是异步的,因此您需要在放置请求上实现成功(和失败)块并在那里运行您的获取请求。但是,您的提取是针对其他实体的,因此不清楚您如何检查/使用返回的身份验证对象。

如果仍有问题,请为映射启用跟踪日志记录以获取其他信息并检查返回到失败块的错误。

最新更新