Wednesday, 5 June 2013

Core Data operations(insert , delete , select) and also get Last inserted id :


Select All data from an entity  in Core Data :


-(NSArray *)selectAllData:(NSString *)entityName{
    AppDelegate  *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    return [context executeFetchRequest:fetchRequest error:nil];

}

Select Data from an entity in core Data with condition :

-(NSArray *)selectConData:(NSString *)entityName dataForCon:(NSMutableDictionary *)dataForCon id_entity:(int)id_e{
    AppDelegate  *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *arrAllKey=[dataForCon allKeys];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id=%d",id_e];
    [fetchRequest setPredicate:predicate];
    return [context executeFetchRequest:fetchRequest error:nil];
    
}



Insert Data in entity : 


-(BOOL)insertData:(NSString *)entityName dataForInsert:(NSMutableDictionary *)dataForInsert{
    AppDelegate  *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *newContact;
    newContact = [NSEntityDescription
                  insertNewObjectForEntityForName:entityName
                  inManagedObjectContext:context];
    NSArray*keys=[dataForInsert allKeys];
    for(int i=0;i<keys.count;i++){
        [newContact setValue:[dataForInsert valueForKey:(NSString *)[keys objectAtIndex:i]] forKey:[keys objectAtIndex:i]];
    }
    
    NSError *error;
    if([context save:&error]){
        return true;
    }else{
        return false;
    }
}

Get Last inserted id in Particular Entity :

-(NSArray *)getLastId:(NSString *)entityName idForTable:(NSString *)key{
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:key ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sort];
    AppDelegate  *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
   [fetchRequest setSortDescriptors:sortDescriptors];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    return[context executeFetchRequest:fetchRequest error:nil];
}


Delete All data from an entity :

-(BOOL)deleteAllData:(NSString *)entityName{
    NSMutableArray *objAll= (NSMutableArray *)[self selectAllData:entityName];
    AppDelegate  *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    for(int i=0;i<objAll.count;i++){
        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        User_info *obj=[objAll objectAtIndex:i];
        [context deleteObject:obj];
        [context save:nil];
    }
    return YES;
}

No comments:

Post a Comment