Iphone 我需要帮助以更好的逻辑重新构造我的方法
更新: 我对这种方法有这样的问题: 默认情况下,除非用户手动更改日期,否则视图会将日期设置为今天,并创建一个以今天的日期作为时间戳的会话 如果我在今天的日期加上运动,一切都很好 如果我选择了一个前一个日期并添加了一个练习,这似乎很好,但似乎练习不仅添加到了前一个日期,而且还添加到了今天 因此,无论我选择之前的日期,exercise实体都会添加到该会话和今天的会话中 因此,当我在tableview中查看今天的会话时,它已经完成了所有练习,因为所有内容都添加到了今天以及手动选择的日期 我怎样才能解决这个问题 原件: 我有一个detailViewController,负责处理核心数据对象会话、练习和设置 该视图的目的是让用户输入有关其健身训练的详细信息。默认情况下,日期设置为今天,但用户可以将其更改为以前的日期。在Iphone 我需要帮助以更好的逻辑重新构造我的方法,iphone,objective-c,core-data,Iphone,Objective C,Core Data,更新: 我对这种方法有这样的问题: 默认情况下,除非用户手动更改日期,否则视图会将日期设置为今天,并创建一个以今天的日期作为时间戳的会话 如果我在今天的日期加上运动,一切都很好 如果我选择了一个前一个日期并添加了一个练习,这似乎很好,但似乎练习不仅添加到了前一个日期,而且还添加到了今天 因此,无论我选择之前的日期,exercise实体都会添加到该会话和今天的会话中 因此,当我在tableview中查看今天的会话时,它已经完成了所有练习,因为所有内容都添加到了今天以及手动选择的日期 我怎样才能解决
createSession
中,当用户按该日期的“完成”按钮时,如果该日期不存在,将创建一个具有该日期时间戳的新会话对象。如果它已经存在,它将获取带有该时间戳日期的对象
问题是,即使用户以后没有输入必要的数据,这也会创建一个新的会话对象。创建训练后,系统会提示用户输入用于训练的重量和重复次数。如果用户未输入此信息(对象集),则存在一个会话对象,该对象没有任何用途,因为它们没有与之相关的练习和设置对象
但我在检查权重/rep之前创建会话对象的原因是,如果获取会话,我希望在视图中显示其属性数据,因此我需要在添加集合/rep之前使用它
我目前的代码是:
-(void)createSession
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"(timeStamp >= %@ && timeStamp <= %@)", targetDateBegins, targetDateEnds]];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"Fetch error: %@", error);
if ([results count])
{
session = (Session *)[results objectAtIndex:0];
}
else
{
session = (Session *)[NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:managedObjectContext];
session.timeStamp = self.picker.date;
}
NSSet *filteredExercisesFromSession=[session.exercises filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@",selectedExerciseName]];
if ([filteredExercisesFromSession count] > 0)
{
self.exercise=[filteredExercisesFromSession anyObject];
}
else
{
self.exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
self.exercise.name = selectedExerciseName;
[session addExercisesObject:exercise];
}
if (![managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(@"Save error: %@", error);
[fetchRequest release];
self.fetchedResultsController = nil;
[setsTableView reloadData];
}
-(IBAction)createSet
{
Set *set = (Set *)[NSEntityDescription insertNewObjectForEntityForName:@"Set" inManagedObjectContext:managedObjectContext];
set.weight = [NSNumber numberWithFloat:weightSelected2];
set.reps = [NSNumber numberWithInt:repSelected];
set.timeStamp = self.picker.date;
[self.exercise addSetsObject:set];
NSError *error = nil;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(@"error: %@", error);
self.fetchedResultsController = nil;
[setsTableView reloadData];
}
-(void)createSession
{
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@”(时间戳>=%@&&timeStamp=%@&&timeStamp这可能比我想写的要脏一点,但它应该能让您很好地了解我对如何解决问题的看法
//Session.m
//Check out the link to PSFoundation and the ActiveRecord Core Data methods
//https://github.com/steipete/PSFoundation/tree/master/activerecord-coredata
NSString * const kSessionLeavingActiveSession = @"kSessionLeavingActiveSession";
NSString * const kSessionChangingActiveSession = @"kSessionChangingActiveSession";
static Session *activeSession = nil;
@implementation Session
// ....
// a static method to retrieve & create if nil the active session
+ (Session *)activeSession {
//if activeSession = nil create a new instance
//reading the code it looks like you only create a new session if there
//isn't a session that already exists for the day chosen in the date picker?
//If that's the case, then it is only possible for there ever to be
//a single instance of an active session, one which is currently in view.
}
+ (Session *)sessionForDate:(NSDate *)date andSetActive:(BOOL)isActive {
//wrap all of your create session logic in here.
//search for a session that exists for this date, and return it if
//it exists, or create a new session if it doesn't.
//send Notification kSessionChangingActiveSession if isActive == YES
//if isActive is true, set it to the active session.
}
+ (void)saveOrDestroyActiveSession {
//register this function as an observer on kSessionChangingActiveSession
//before the active session is changed, you will be able to determine
//if the session has anything associated with it, and persist it, or
//destroy it.
//determine if it has exercises assigned to it.
//if it does, save the context.
//if it doesn't destroy the active session and set activeSession to nil
}
+ (void)saveActiveSession {
//register this function as an observer on kSessionLeavingActiveSession.
//If you are leaving an area of the application where you are working on
//the session, you want to send this notification so that you can save
//the session if you have added exercises.
//you don't want to delete it though, if you can still go back to the
//active session, for example, if it's the active session view controller
//on a tab controller.
}
// the rest of your session methods
@end
//your app delegate
- (void)applicationWillResignActive:(UIApplication *)application {
//send a notification that you are leaving the active session
}
//viewController.m
//modify your fetched results controller to pull it's list of exercises from the
//active session, in case it's not saved you can still retrieve the exercises associated
//with it.
- (void)viewDidUnload {
//send a notification that you are leaving the active session
}
- (void)createSession {
Session *activeSession = [Session sessionForDate:self.picker.date andSetActive:YES];
//code to add exercises to session
//if you've added exercises go ahead and save.
//if you haven't let the observers take care of persisting or destroying it.
}
你试过在上问吗?我不知道那个网站,我现在正在查看。谢谢Dear Travis,我尝试实现此代码,但遇到了一些问题。你能帮我完成一下吗?另外,我是否必须添加active record framework类才能使用此代码?我还用你的su中的新代码更新了问题虽然我是一个很好的老师,但我一直在思考如何完成其中的一些。谢谢。
//Session.m
//Check out the link to PSFoundation and the ActiveRecord Core Data methods
//https://github.com/steipete/PSFoundation/tree/master/activerecord-coredata
NSString * const kSessionLeavingActiveSession = @"kSessionLeavingActiveSession";
NSString * const kSessionChangingActiveSession = @"kSessionChangingActiveSession";
static Session *activeSession = nil;
@implementation Session
// ....
// a static method to retrieve & create if nil the active session
+ (Session *)activeSession {
//if activeSession = nil create a new instance
//reading the code it looks like you only create a new session if there
//isn't a session that already exists for the day chosen in the date picker?
//If that's the case, then it is only possible for there ever to be
//a single instance of an active session, one which is currently in view.
}
+ (Session *)sessionForDate:(NSDate *)date andSetActive:(BOOL)isActive {
//wrap all of your create session logic in here.
//search for a session that exists for this date, and return it if
//it exists, or create a new session if it doesn't.
//send Notification kSessionChangingActiveSession if isActive == YES
//if isActive is true, set it to the active session.
}
+ (void)saveOrDestroyActiveSession {
//register this function as an observer on kSessionChangingActiveSession
//before the active session is changed, you will be able to determine
//if the session has anything associated with it, and persist it, or
//destroy it.
//determine if it has exercises assigned to it.
//if it does, save the context.
//if it doesn't destroy the active session and set activeSession to nil
}
+ (void)saveActiveSession {
//register this function as an observer on kSessionLeavingActiveSession.
//If you are leaving an area of the application where you are working on
//the session, you want to send this notification so that you can save
//the session if you have added exercises.
//you don't want to delete it though, if you can still go back to the
//active session, for example, if it's the active session view controller
//on a tab controller.
}
// the rest of your session methods
@end
//your app delegate
- (void)applicationWillResignActive:(UIApplication *)application {
//send a notification that you are leaving the active session
}
//viewController.m
//modify your fetched results controller to pull it's list of exercises from the
//active session, in case it's not saved you can still retrieve the exercises associated
//with it.
- (void)viewDidUnload {
//send a notification that you are leaving the active session
}
- (void)createSession {
Session *activeSession = [Session sessionForDate:self.picker.date andSetActive:YES];
//code to add exercises to session
//if you've added exercises go ahead and save.
//if you haven't let the observers take care of persisting or destroying it.
}