how to fetch & store any column from coredata to array

Posted by user440485 on Stack Overflow See other posts from Stack Overflow or by user440485
Published on 2011-01-13T05:43:16Z Indexed on 2011/01/13 5:53 UTC
Read the original article Hit count: 221

Filed under:
|

Hi All, I am working on page(Lesson) has button called "AddToFavorite".
when i click on this button lessonID,lessonHeading is added in the Coredata.

- (NSFetchedResultsController *)fetchedResultsController {

    // Set up the fetched results controller if needed.


    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }


    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favorites" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lessonHeading" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"lessonHeading" cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    self.fetchedResultsController.delegate = self;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];
    return fetchedResultsController;

}    

-(void)addToFavorites
{


    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"Lesson has been added  successfully to your Favorite List" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    //Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller's context.
    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
    self.addingManagedObjectContext = addingContext;

    [addingManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];

    Favorites *fav = [NSEntityDescription insertNewObjectForEntityForName:@"Favorites" inManagedObjectContext:addingContext];

    fav.lessonID=[[data objectAtIndex:count]objectForKey:@"LessonID"];
    fav.lessonTitle=[[data objectAtIndex:count]objectForKey:@"LessonTitle"];    


    [self didFinishWithSave:YES];
    [addingContext release];
}

My problem is that i want to show an alert if this lessonID is already existing in the
coreData.
show guide me how i can match the current page lessonID with the existing lessonID's in the CoreData.
I am using CoreData 1st time.so sory for any mistake ...& thanks for ypur help frnds.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk-4.0