我目前正在开发iOS应用。我试图在表上插入数据后从表中获取数据。这是代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notification *notification = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([notification.isRead isEqualToNumber:@NO]) {
[self sendNotificationData:notification];
notification.isRead = @YES;
}
[self insertPost:notification.notificatableId];
Post *post = [self fetchObjectfrom:@"Post"
withPredicate:[NSPredicate predicateWithFormat:@"objectId == %@", notification.notificatableId]];
}
- (void)insertPost:(NSString *)postId
{
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:postId, @"notification[post_id]", nil];
[[AkdemiaAPIClient sharedClient] POST:[NSString stringWithFormat:@"/notifications/%@/get_post",postId] parameters:parameters
success:^(NSURLSessionDataTask *task, id JSON) {
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:@"PostNotificationsDetailsNotificationName"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"PostNotificationsDetailsSucceedNotificationName"
object:nil];
AkdemiaSyncEngine *syncEngine = [[AkdemiaSyncEngine alloc] init];
[syncEngine processJSONDataRecordsIntoCoreData:JSON forComponent:kFeed];
[[AkdemiaCoreDataController sharedInstance] saveBackgroundContext];
[[AkdemiaCoreDataController sharedInstance] saveMasterContext];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"PostNotificationsDetailsErrorNotificationName"
object:nil];
}];
}
问题是当我选择通知并尝试在插入 后立即 时,因为它似乎不在此表上,但如果我刷新控制器并选择相同的通知,则帖子存在且一切正常。
我试图找到类似的东西:
[tableView reloadData]
但这适用于请求而不是表。
答案 0 :(得分:0)
您应该实现NSFetchedResultsController
委托方法。在API调用的回调中,您可以将检索到的项目插入Core Data并保存。然后应该调用委托方法并根据indexPath
插入/更新/删除表视图单元格。 Xcode模板中有一个参考实现(Master-Detail,检查Core Data)。