我对coredata很新,并且一直困在一个问题上。 任何帮助将不胜感激。
信息: 我有一个核心数据应用程序与实体; 列表和任务。 列表和任务具有一对多的关系。
Task.h
@class List;
@interface Task : NSManagedObject
@property (nonatomic, retain) NSString * task;
@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) List *list;
@end
List.h
@class Task;
@interface List : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * dateCreated;
@property (nonatomic, retain) NSNumber * sortOrder;
@property (nonatomic, retain) NSSet *task;
@end
@interface List (CoreDataGeneratedAccessors)
- (void)addTaskObject:(Task *)value;
- (void)removeTaskObject:(Task *)value;
- (void)addTask:(NSSet *)values;
- (void)removeTask:(NSSet *)values;
@end
我使用;
创建列表 NSManagedObjectContext *context = [self managedObjectContext];
if ([self.listTextField.text length] == 0) { // Quit here if no text is entered
[self dismissViewControllerAnimated:YES completion:nil];
return;
}
// Create a new list.
// Create an NSManagedObject for our database entity.
list = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:context];
// Add the new task to the object (which in turns adds to our database).
[list setValue:self.listTextField.text forKey:@"name"];
// Get current date and time.
NSDate *todayDate = [NSDate date];
// Add the date to the object (which in turns adds to our database).
[list setValue:todayDate forKey:@"dateCreated"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
我可以使用以下代码使用cellaccessorybuttontapped;
更新已创建的列表NSManagedObjectContext *context = [self managedObjectContext];
if ([self.listTextField.text length] == 0) {
// Delete object from database.
[context deleteObject:self.list];
NSError *error = nil;
// Save the action to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
} else {
// Update existing task.
[self.list setValue:self.listTextField.text forKey:@"name"];
// Get current date and time.
NSDate *todayDate = [NSDate date];
// Add the date to the object (which in turns adds to our database).
[list setValue:todayDate forKey:@"dateCreated"];
NSError *error = nil;
// Save the action to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
然后我可以导航到一个列表。
我的问题是如何为我刚刚导航的列表创建任务? 已经过去2天了,我无法在谷歌上找到任何东西。
据我所知,我已添加;
@property (strong, nonatomic) List *selectedList;
我现在将此作为我的保存方法
NSManagedObjectContext * context = [self managedObjectContext];
// Saving a new task.
Task *task = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:context];
task.task = self.taskText.text;
task.note = self.noteText.text;
task.list = self.selectedList;
NSLog(@"The selected list is: %@", [self.selectedList description]);
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self.selectedList addTaskObject:task];
[self dismissViewControllerAnimated:YES completion:nil];
创建新任务但在所有列表中创建。 这是否可行,我不是根据他们的列表获取任务?
当我导航到列表时,这是我的提取请求:
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create a fetch request.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Create an entity so fetch the data from.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:self.managedObjectContext];
// Set the entity of the fetch request.
[fetchRequest setEntity:entity];
// Set the amount to be fetched at a time
[fetchRequest setFetchBatchSize:20];
// Create a sort descriptor.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"task" ascending:NO];
// Attach the sort descriptor to the fetch request.
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create a fetch result controller using the fetch request
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
theFetchedResultsController.delegate = self;
// Perform fetch.
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Handle error.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
return fetchedResultsController;
答案 0 :(得分:1)
这就是实体标题中的这些方法的用途:
- (void)addTaskObject:(Task *)value;
- (void)removeTaskObject:(Task *)value;
- (void)addTask:(NSSet *)values;
- (void)removeTask:(NSSet *)values;
您将创建一个新的任务实体:
Task *t = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:context];
然后像对待列表对象一样填写其中的字段:
t.task = @"Whatever";
t.note = @"Whatever Note";
t.list = currentlySelectedListItem; // whatever that happens to be -- it will be a (List *)something
然后,您要将任务对象添加到列表中:
[currentlySelectedListItem addTask:t];
然后保存上下文&你已经完成了。
这里的关键是,您可以通过向NSSet中包含的Task值集添加任务来有效地更新List对象。并且t.list将包含指向父List对象的指针。
在我看来,你把它布置得很好(我假设:
@property (nonatomic, retain) List *list;
是与父列表的关系,而不仅仅是您定义的另一个值;看起来就是这样。)
答案 1 :(得分:0)
您需要跟踪已导航到的列表。一种方法是创建一个名为parentList
的属性,如此
@property(nonatomic,strong) List *parentList
在视图控制器中创建任务。在导航到视图控制器之前设置此属性。
在任务视图控制器中,使用List
执行类似于Task *reqdTask = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:context];
对象的插入,然后在按下Save
按钮后设置所有值。
[parentList addTaskObject: reqdTask];
你做完了。这将在任务实体中创建Task
并将其映射到List
实体。希望这会有所帮助。
<强> **编辑*** 强>
在保存上下文之前,您需要执行此操作[parentList addTaskObject: reqdTask];
。
在NSFectResultsController
中添加此内容
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"list = %@",self.parentList]];
所以它会是这样的
// Create a fetch request.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Create an entity so fetch the data from.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:self.managedObjectContext];
// Set the entity of the fetch request.
[fetchRequest setEntity:entity];
// Set the amount to be fetched at a time
[fetchRequest setFetchBatchSize:20];
// Create a Predicate.
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"list = %@",self.parentList]];
//continue
这将带来与所选列表相关联的任务。