如何使用核心数据同时向两个表发送数据?

时间:2011-06-29 03:22:02

标签: iphone objective-c cocoa-touch ios4 core-data

我正在尝试将数据添加到两个表中。为此,有必要写这一行,

[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];    

两次,每张桌子一次?

如果我使用它一次,它不会将数据存储到第二个表。我哪里错了?

bucketlistviewcontroller.m

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"ADD" style:UIBarButtonSystemItemAdd target:self action:@selector(addListItem)];
        [self.navigationItem setRightBarButtonItem:addButton];
        [addButton release];



//this function adds new cell when add button is clicked
- (void)addListItem{
    [NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];
}

noteseditcontroller.m

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];


    if(saveChanges){
        // Save any changes to note
        //noteToEdit object of Note class when editing is there
        //newNote is object of Note class when new note is being generated

        if([[noteTextView text] length] > 0){
            if(noteToEdit || listcell){
                // Edit Note
                if([[titleField text] length] <= 0)
                    [noteToEdit setNoteTitle:[noteTextView text]];
                else
                    [noteToEdit setNoteTitle:[titleField text]];
                [noteToEdit setNoteText:[noteTextView text]];                

            } else {
                // New Note
            Note *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context];
                if([[titleField text] length] <= 0)
                    [newNote setNoteTitle:[noteTextView text]];
                else
                    [newNote setNoteTitle:[titleField text]];

                [newNote setNoteText:[noteTextView text]];
                listcell=[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];       
//here list cell is another table and this line adds extra cell to my view which should not happen.                    

                [listcell setItemText:@"Note"];


            }
        } else {
            // Remove note (zero length)
            if(noteToEdit){
                [context deleteObject:noteToEdit];
            }
        }
    }

    NSError *error = nil; 
    if (![context save: &error]) {
        // Couldn't save
    }


}

0 个答案:

没有答案