我剛剛安裝了框架restkit 0.9.3並遵循討論板示例。那麼,一切都很好,但是當我嘗試使用核心數據時,即使在聲明他的primaryKeyAttribute(userID)後,我的用戶NSManagedObject類也會重複。例如,當我向我的網絡服務器發送登錄請求時,我返回{"user":{"id":1, "username":"teste", ...}}
..但它似乎在每次調用objectLoader:didLoadObjects時創建一個新行。primaryKeyAttribute不工作Restkit/Core Data
用戶表:
示例代碼:
〜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);
}
我在做什麼錯?
也許應該叫 - (NSManagedObject *)findOrCreateInstanceOfEntity: (NSEntityDescription *)實體withPrimaryKeyAttribute: (的NSString *)primaryKeyAttribute andValue:(ID)primaryKeyValue; ? – mateusmaso