我正在使用 RestKit 将 JSON 响应映射到 CoreData。我有一个带有 UITextFields 和“保存”按钮的表格视图,它应该将数据发布到服务器并保存它。服务器数据库已正确更改,但 tableView 使用旧数据重新加载。只有当我返回然后再次进入视图时,才会加载正确的数据。
我花了几个小时寻找解决方案,但我尝试过的没有任何效果。如果我将 [self loadData] 放在 POST 行之后,应用程序会崩溃,可能是因为 POST 尚未完成并且存在冲突。如果我将 [self.tableview reloadData] 放在该行中,它也不起作用。
这是我的代码。我正在使用 NSManagedObject 来保存数据,但将新创建的 NSObject (CommunityOtherProfile) 发送回服务器。
这是我的映射:
// CommunityProfile Object
RKManagedObjectMapping *profileMapping = [RKManagedObjectMapping mappingForClass:[CommunityProfile class] inManagedObjectStore:objectStore];
profileMapping.primaryKeyAttribute = @"uid";
[profileMapping mapKeyPath:@"uid" toAttribute:@"uid"];
[profileMapping mapKeyPath:@"nickname" toAttribute:@"nickname"];
[profileMapping mapKeyPath:@"hometown" toAttribute:@"hometown"];
[profileMapping mapKeyPath:@"male" toAttribute:@"male"];
[profileMapping mapKeyPath:@"age" toAttribute:@"age"];
[profileMapping mapKeyPath:@"description" toAttribute:@"profileDescription"];
[[RKObjectManager sharedManager].mappingProvider setMapping:profileMapping forKeyPath:@"communityProfile"];
mappingForSerialization = [profileMapping inverseMapping];
[[RKObjectManager sharedManager] setSerializationMIMEType:RKMIMETypeJSON];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:mappingForSerialization
forClass:[CommunityProfile class]];
[[RKObjectManager sharedManager].router routeClass:[CommunityProfile class] toResourcePath:@"/community/profile/getMyProfile/" forMethod:RKRequestMethodGET];
//[[RKObjectManager sharedManager].router routeClass:[CommunityProfile class] toResourcePath:@"/community/profile/postMyProfile/" forMethod:RKRequestMethodPOST];
// CommunityOtherProfile Object
RKObjectMapping *profileMapping2 = [RKObjectMapping mappingForClass:[CommunityOtherProfile class]];
[profileMapping2 mapKeyPath:@"uid" toAttribute:@"uid"];
[profileMapping2 mapKeyPath:@"nickname" toAttribute:@"nickname"];
[profileMapping2 mapKeyPath:@"hometown" toAttribute:@"hometown"];
[profileMapping2 mapKeyPath:@"male" toAttribute:@"male"];
[profileMapping2 mapKeyPath:@"age" toAttribute:@"age"];
[profileMapping2 mapKeyPath:@"description" toAttribute:@"profileDescription"];
[[RKObjectManager sharedManager].mappingProvider setMapping:profileMapping2 forKeyPath:@""];
mappingForSerialization = [profileMapping2 inverseMapping];
[[RKObjectManager sharedManager] setSerializationMIMEType:RKMIMETypeJSON];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:mappingForSerialization
forClass:[CommunityOtherProfile class]];
[[RKObjectManager sharedManager].router routeClass:[CommunityOtherProfile class] toResourcePath:@"/community/profile/postMyProfile/" forMethod:RKRequestMethodPOST];
这是 POST 发生的地方:
- (void) save_clicked:(id)sender {
CommunityOtherProfile * newProfile = [[CommunityOtherProfile alloc] init];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * ageNumber = [f numberFromString:age.text];
NSString *tempDescription = @"Platzhalter (noch nicht implementiert!)";
newProfile.male = isMale;
newProfile.profileDescription = tempDescription;
newProfile.nickname = nickname.text;
newProfile.hometown = hometown.text;
newProfile.age = ageNumber;
// post Object
[[RKObjectManager sharedManager] postObject:newProfile usingBlock:^(RKObjectLoader *loader){
loader.targetObject = nil;
loader.delegate = profileDataLoader;
}];
// I have tried to insert reloadData here }
同一 TableViewController 类中的其他重要方法:
- (void) refreshView: (id) sender{
NSFetchRequest *request = [CommunityProfile fetchRequest];
prof = [[CommunityProfile objectsWithFetchRequest:request] objectAtIndex:0];
self.uid = prof.uid;
self.nickname.text = prof.nickname;
self.age.text = [NSString stringWithFormat:@"%d", [prof.age intValue]];
self.hometown.text = prof.hometown;
//self.description.text = prof.description;
if(prof.male == [NSNumber numberWithInt:1]) {
[self clickMale:self.male];
}
else if(prof.male == [NSNumber numberWithInt:0]) {
[self clickFemale:self.female];
} }
- (void) loadData{
profileDataLoader = [[CommunityProfileDataLoader alloc] initWithDelegate:self];
[profileDataLoader loadData];
最后,我的 dataLoader 类:
//
// CommunityProfileDataLoader.m
//
#import "CommunityProfileDataLoader.h"
#import "CommunityProfile.h"
@interface AbstractDataLoader()
@property CommunityEditProfileViewController *view;
@end
@implementation CommunityProfileDataLoader
@synthesize responseCode = _responseCode;
@synthesize view;
- (void) loadData{
[super loadData];
HCLog(DEBUG_MODE,@"Loading Profile Data");
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/community/profile/getMyProfile/" delegate:self];
}
- (void) triggerDelegate{
HCLog(DEBUG_MODE,@"CommunityProfileDataLoader Triggering Delegate for %@", [super delegate]);
[[super delegate] refreshView:self];
} /*...*/
如果你们能帮忙,我会很高兴:)