iphone sdk - 核心数据:关于谓词,实体等

时间:2011-04-24 14:49:10

标签: iphone core-data predicate

我在iphone中使用带有核心数据的数据库。我想通过搜索获得一个列信息.. 我的示例代码是;

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Groups : NSManagedObject {
   @private
}
@property (nonatomic, retain) NSString * GroupID;
@property (nonatomic, retain) NSString * userID;
@property (nonatomic, retain) NSString * memberID;
@property (nonatomic, retain) NSString * memberNAME;
@property (nonatomic, retain) NSString * memberLNAME;
@property (nonatomic, retain) NSString * memberNUMBER;
@property (nonatomic, retain) NSString * memberBIRTH;
@property (nonatomic, retain) NSString * memberMARRIAGE;

@end


-(void)GetDataFromDB{


        app = (CepostaAppDelegate*)[[UIApplication sharedApplication] delegate];

        root = (RootViewController*)
                   ([app.navigationController.viewControllers objectAtIndex:0]);

        NSFetchRequest *request = [[NSFetchRequest alloc] init];

        NSEntityDescription *entity = [NSEntityDescription 
                  entityForName:@"Groups"
                  inManagedObjectContext:app.managedObjectContext];


        NSPredicate *predicate = [NSPredicate 
                  predicateWithFormat:@"(GroupID IN %@)",SendGrupList];

        [request setEntity:entity];
        [request setPredicate:predicate];

        NSError *error; 
        NSMutableArray *TempArr = [[NSMutableArray alloc] initWithArray:
            [app.managedObjectContext executeFetchRequest:request error:&error]];

        [request release];

        for (gObje in TempArr) { //gObje is Groups's Object
            NSString *GroupsMemNum = [[NSString alloc] 
                                    initWithString:gObje.memberNUMBER];
            [gGonder.TeLList addObject:GroupsMemNum];
            NSLog(@"%@",GroupsMemNum);
            [GroupsMemNum release];
        }  
}

我使用SendGrupList中的谓词进行搜索(它有组ID),但TempArr返回db中的所有数据,我怎样才能获得一个列信息?

2 个答案:

答案 0 :(得分:0)

你必须使用像

这样的引用
(refGroupTableName.groupId IN %@"),SendGrupList

答案 1 :(得分:0)

你的SendGrupList是什么?如果它是一个数组,它应该工作。 这是一个工作示例,其中SendGrupListNSMutableArray

[SendGrupList addObject:data]; 
    NSError *error = nil;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate 
                         predicateWithFormat:@"GroupID IN %@",SendGrupList];

    [fetchRequest setPredicate:predicate];

    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"Groups"
                                inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [managedObjectContext 
                              executeFetchRequest:fetchRequest error:&error];