RESTKit を使用して、JSON 応答を CoreData にマップしています。UITextFields と「保存」ボタンを備えたテーブルビューがあります。これは、データをサーバーに POST して保存することになっています。サーバー データベースは正しく変更されますが、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];
} /*...*/
あなたたちが助けてくれたら本当に嬉しいです:)