Ios 使用NSPredicate的NSFetchRequest
我正试图通过核心数据数据库以以下方式创建筛选搜索的所有结果的NSArray:Ios 使用NSPredicate的NSFetchRequest,ios,Ios,我正试图通过核心数据数据库以以下方式创建筛选搜索的所有结果的NSArray: NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"AllFiles" inManagedObjectContext:context]; [fetchR
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"AllFiles" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"userID == %@", userID];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"folderNumber == %d", folderNumber];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"fileType == %@", type];
NSPredicate *fetchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2, p3]];
[fetchRequest setPredicate:fetchPredicate];
NSError *error;
NSArray* returnArray = [context executeFetchRequest:fetchRequest error:&error];
AllFiles *newFile = [NSEntityDescription
insertNewObjectForEntityForName:@"AllFiles"
inManagedObjectContext:context];
newFile.userID = userID;
newFile.folderNumber = [NSString stringWithFormat:@"%d", folderNumber];
....
文件的创建方式如下:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"AllFiles" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"userID == %@", userID];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"folderNumber == %d", folderNumber];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"fileType == %@", type];
NSPredicate *fetchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2, p3]];
[fetchRequest setPredicate:fetchPredicate];
NSError *error;
NSArray* returnArray = [context executeFetchRequest:fetchRequest error:&error];
AllFiles *newFile = [NSEntityDescription
insertNewObjectForEntityForName:@"AllFiles"
inManagedObjectContext:context];
newFile.userID = userID;
newFile.folderNumber = [NSString stringWithFormat:@"%d", folderNumber];
....
如您所见,我正在尝试过滤给定某些参数的结果:
@"userID == %@", userID
但是,将返回所有结果,而不进行筛选。。。知道为什么吗?如果您使用的是OR谓词,那么这三个谓词中至少有一个是匹配的。我怀疑
[NSPredicate predicateWithFormat:@“fileType==%@”,type]
正在收集您的所有文件
试着合成一个像这样的
NSPredicate *idPredicate = [NSPredicate predicateWithFormat:@"userID == %@", userID];
NSPredicate *folderNumPredicate = [NSPredicate predicateWithFormat:@"folderNumber == %d", folderNumber];
NSPredicate *typePredicate = [NSPredicate predicateWithFormat:@"fileType == %@", type];
NSPredicate *fetchPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[idPredicate, folderNumPredicate, typePredicate];
[fetchRequest setPredicate:fetchPredicate];
请记住,
和predicatewithsubpredicates
和或predicatewithsubpredicates
返回一个NSPredicate
,因此,如果需要,它们可以以复杂的方式组合在一起。如果您使用的是OR谓词,那么这三个谓词中至少有一个是匹配的。我的钱存档了type@WarrenBurton谢谢你指出,这应该是一个和。如何使其成为AND?等等,AND谓词,子预测可能会执行此任务。仅供参考,您不必以编程方式创建fetch请求。您可以直接在CoreData模型中定义它。