coredata模糊查詢

    NSManagedObjectContext *context = self.myAppDelegate.managedObjectContext;
    // 限定查詢結果的數量
    //setFetchLimit
    // 查詢的偏移量
    //setFetchOffset
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //
    //    [fetchRequest setFetchLimit:pageSize];
    //    [fetchRequest setFetchOffset:currentPage];
    //
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Lists" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSPredicate * qcmd ;
    if (![str isEqualToString:@""]) {
        NSString *attributeName = @"listName";
        NSString *attributeValue = [NSString stringWithFormat:@"*%@*",str];
        qcmd = [NSPredicate predicateWithFormat:@"%K like %@",attributeName,attributeValue];
    }
    [fetchRequest setPredicate:qcmd];
    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    NSMutableArray *resultArray = [NSMutableArray array];
    for (Lists *list in fetchedObjects) {
        ListsModel *listModel = [[ListsModel alloc]init];
        listModel.listName = list.listName;
        listModel.isFinished = list.isFinished;
        listModel.endTime = list.endTime;
        [resultArray addObject:listModel];
    }
    return resultArray;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章