ここ数週間、私はRestkit(v0.10.0)とコアデータを学び、これらの優れたツールで可能性は無限に広がります。問題は、ここで全体像を見る方法に少し圧倒されていることです。また、Restkitの更新のペースが非常に速いため、ほとんどのチュートリアル/デモコードは古く、正しく機能しなくなりました。
リモートサーバー上のjsonからのデータでテーブルビューを埋めることができました。キャッシュと組み合わせてリモートデータをリードする方法も検討しましたが、NSManagedObjectContext / NSEntityDescription(コアデータ)と、POSTコマンドを使用した場合のRestkitでの動作に苦労しています。
私が正しく理解していれば、レコードはCore Dataで作成され(コメント行の後//新しいインスタンスを作成します)、その後、そのデータを使用してPOSTリクエストが作成され、レコードがサーバーに投稿されます。
このコードはサーバー上に新しいレコードを作成するために使用されていますが、コードが実行されると(サーバー上にレコードが作成されているのがわかります)、それに応じてテーブルビューが更新されないため、テーブルビューは更新されないため、新しいレコードは次のようになります。アプリを再起動すると最初に表示されます。サーバーからデータを手動で更新することも役に立ちません。
うまくいけば、誰かが私にいくつかのポインタ、またはおそらくRestkit/コアデータとPOSTを組み合わせたチュートリアルを教えてくれるでしょう。ありがとう!
- (void)createGoalWithName:(NSString *)name andDescription:(NSString *)goalDescription
{
Goal* goal = [Goal object];
goal.identifier = 0;
goal.name = name;
goal.goalDescription = goalDescription;
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[self saveContext];
[[RKObjectManager sharedManager].router routeClass:[Goal class] toResourcePath:@"/api/goals" forMethod:RKRequestMethodPOST];
[[RKObjectManager sharedManager] postObject:goal delegate:self];
[self.tableView reloadData];
}
- (void)saveContext {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate.
You should not use this function in a shipping application,
although it may be useful during development.
If it is not possible to recover from the error,
display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}