PUT操作响应未保存在Core Data Store中

时间:2013-11-09 15:10:24

标签: ios objective-c restkit restkit-0.20

我正在尝试使用RestKit使用github对用户进行身份验证,并将响应保存回Core数据以挂起oauth令牌。响应将以200或201返回,这很好,但没有任何内容保存回上下文。我假设有一些我想念的小东西,但我无法弄明白。这是正在发生的事情:

这发生在viewDidLoad

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

1 个答案:

答案 0 :(得分:0)

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

如果仍有问题,请打开映射的跟踪日志记录以获取其他信息,并检查返回到故障块的错误。