【发布时间】:2012-04-19 18:55:26
【问题描述】:
在过去的几周里,我正在学习 Restkit (v0.10.0) 和核心数据,使用这些出色的工具可以提供无穷无尽的可能性。问题是我对如何在这里看到更大的图景有点不知所措。而且由于 Restkit 的更新速度非常快,大多数教程/演示代码已经过时,不再正常工作。
我已经设法让我的 tableview 充满了远程服务器上我的 json 中的数据。我还研究了如何使远程数据与缓存相结合现在工作,但我正在努力解决 NSManagedObjectContext/NSEntityDescription(核心数据)以及在使用 POST 命令时它如何与 Restkit 一起工作。
如果我理解正确,记录是在 Core Data 中创建的(在注释行 // Create a new instance 之后),然后该数据用于创建 POST 请求,以便将记录发布到服务器。
此代码用于在服务器上创建新记录,但是当代码执行时(我看到在我的服务器上创建了一条记录),但我的 tableview 没有相应更新,table view 没有更新,因此重新启动应用程序时,新记录首先可见。从服务器手动刷新数据也无济于事。
希望有人可以给我一些指导,或者可能是一个包含 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();
}
【问题讨论】:
-
我一直在研究同样类型的事情。 Here是RestKit和Core data教程的链接。
-
是的,我也看过那个教程。问题是它已经很老了,而且 RestKit 的大部分工作方式都随着时间的推移而发生了变化。不过谢谢!
-
如果您想得到答案,我认为您需要详细说明如何填充
UITableView。上面的代码显示了(可能)有效的持久性机制,但您没有解释如何尝试检索此数据以显示在表中。 :-) -
我试过使用RestKit,也遇到了同样的问题。经过一周的彻底沮丧后,我放弃了并打包了自己的服务,该服务功能较少,但更易于使用。如果您有兴趣尝试一下,希望得到反馈。 github.com/loudin/RSAPI
-
我现在正在尝试您的解决方案 Michael D。谢谢!
标签: iphone ios core-data restkit