我刚刚安装了框架 restkit 0.9.3 并按照讨论板示例进行操作。好吧,一切都很好,但是当我尝试使用 Core Data 时,即使在声明了他的 primaryKeyAttribute (userID) 之后,我的 User NSManagedObject 类仍在复制。例如,当我向我的网络服务器发送登录请求时,我返回{"user":{"id":1, "username":"teste", ...}}
.. 但它似乎每次调用 objectLoader:didLoadObjects 时都会创建一个新行。
用户表:
示例代码:
~ AppDelegate.m didFinishLaunching
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];
userMapping.primaryKeyAttribute = @"userID";
userMapping.setDefaultValueForMissingAttributes = YES; // clear out any missing attributes (token on logout)
[userMapping mapKeyPathsToAttributes:
@"id", @"userID",
@"email", @"email",
@"username", @"username",
@"password", @"password",
nil];
[objectManager.mappingProvider registerMapping:userMapping withRootKeyPath:@"user"];
~ User.m loginWithDelegate
- (void)loginWithDelegate:(NSObject<UserAuthenticationDelegate>*)delegate {
_delegate = delegate;
[[RKObjectManager sharedManager] postObject:self delegate:self block:^(RKObjectLoader* loader) {
loader.resourcePath = @"/login";
loader.serializationMapping = [RKObjectMapping serializationMappingWithBlock:^(RKObjectMapping* mapping) {
[mapping mapAttributes:@"username", @"password", nil];
}];
}];
}
~ User.m didLoadObjects (RKObjectLoaderDelegate)
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray *)objects {
if ([objectLoader wasSentToResourcePath:@"/login"]) {
[self loginWasSuccessful];
}
NSLog(@"number of user rows: %i", [User findAll].count);
}
我究竟做错了什么?