关于NSPredicate与核心数据

时间:2013-07-04 16:57:32

标签: ios core-data nspredicate

我的项目简介:某些数据(所有静态数据)存储在核心数据中。该应用程序有一个要搜索的文本字段。我正在使用NSPredicate将搜索字符串与核心数据中存在的任何数据进行匹配。一旦找到,它应该在点击搜索按钮时显示在表格视图中。

阻止程序:获取结果控制器不返回任何内容,因此不显示数据

以下是我正在做的事情。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    appDelegate = (ADAppDelegate*)[[UIApplication sharedApplication]delegate];
    loginViewController = [[ADAssociateLoginViewController alloc]init];
    self.searchGUIDTextField.hidden = YES;
    self.searchPagecountTextField.hidden = YES;
    self.searchUserIdTextField.hidden = YES;
    self.searchResultsTableView.hidden = YES;
    self.searchResultsArray = [[NSMutableArray alloc]init];
    //self.fetchedResultsController = [[NSFetchedResultsController alloc]init];

     firstName = [NSMutableArray arrayWithObjects:@"Dipanjan", @"Lokesh", @"Manish", @"Manpreet", nil];
    lastName = [NSMutableArray arrayWithObjects:@"Dutta", @"Gahlawat", @"Bhardwaj", @"Kaur", nil];
    globalID = [NSMutableArray arrayWithObjects:@"86077", @"86088", @"95632", @"95741", nil];
    location = [NSMutableArray arrayWithObjects:@"Offshore", @"Offshore", @"Offshore", @"Offshore", nil];
    subCircle = [NSMutableArray arrayWithObjects:@"TME", @"MEA", @"TME", @"AP", nil];
    locationTelNo = [NSMutableArray arrayWithObjects:@"001", @"010", @"002", @"012", nil];
    extension = [NSMutableArray arrayWithObjects:@"7777", @"7777", @"7887", @"9254", nil];
    cityName = [NSMutableArray arrayWithObjects:@"Bangalore", @"Hyderabad", @"Bangalore", @"Chennai", nil];
countryName = [NSMutableArray arrayWithObjects:@"India", @"India", @"India", @"India", nil];
    mobileNo = [NSMutableArray arrayWithObjects:@"123", @"234", @"345", @"456", nil];
    rmName = [NSMutableArray arrayWithObjects:@"SP", @"KM", @"KM", @"SKG", nil];
    rmEmail = [NSMutableArray arrayWithObjects:@"s_p", @"k_m", @"k_m", @"s_kg", nil];
    aicName = [NSMutableArray arrayWithObjects:@"MK", @"MK", @"AB", @"BC", nil];
    aicEmail = [NSMutableArray arrayWithObjects:@"m_k", @"m_k", @"a_b", @"b_c", nil];

    int i;
    NSManagedObject* associateSearchObject = [NSEntityDescription insertNewObjectForEntityForName:@"AssociateDetails" inManagedObjectContext:appDelegate.managedObjectContext];
    for (i = 0; i < [firstName count]; i++) {
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [firstName objectAtIndex:i]] forKey:@"firstName"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [lastName objectAtIndex:i]] forKey:@"lastName"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [globalID objectAtIndex:i]] forKey:@"gid"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [location objectAtIndex:i]] forKey:@"location"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [subCircle objectAtIndex:i]] forKey:@"subCircle"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [locationTelNo objectAtIndex:i]] forKey:@"locationTelNo"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [extension objectAtIndex:i]] forKey:@"extension"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [cityName objectAtIndex:i]] forKey:@"city"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [countryName objectAtIndex:i]] forKey:@"country"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [mobileNo objectAtIndex:i]] forKey:@"mobileNo"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [rmName objectAtIndex:i]] forKey:@"rm"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [rmEmail objectAtIndex:i]] forKey:@"rmEmail"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [aicName objectAtIndex:i]] forKey:@"aic"];
        [associateSearchObject setValue:[NSString stringWithFormat:@"%@", [aicEmail objectAtIndex:i]] forKey:@"aicEmail"];

        NSLog(@"associateSearchObject has %@", associateSearchObject);
    }

    NSError* error;
    NSFetchRequest* fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription* entity = [NSEntityDescription entityForName:@"AssociateSearch" inManagedObjectContext:appDelegate.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray* fetchedObjects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject* info in fetchedObjects) {
       self.searchUserIdTextField.text = [info valueForKey:@"userId"];
       self.searchPagecountTextField.text = [info valueForKey:@"pageCount"];
        self.searchGUIDTextField.text = [info valueForKey:@"guid"];
    }
}

-(IBAction)searchAssociate:(id)sender{
   NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

   NSEntityDescription *entity = [NSEntityDescription entityForName:@"AssociateDetails"  inManagedObjectContext:appDelegate.managedObjectContext];
   [fetchRequest setEntity:entity];

    predicate = [NSPredicate predicateWithFormat:@"firstName == %@ || lastName == %@ ||gid == %@ || location == %@ ||subCircle == %@ ||locationTelNo == %@ ||extension == %@ ||city == %@ ||country == %@ ||mobileNo == %@ ||rm == %@ ||rmEmail == %@ ||aic == %@ ||aicEmail == %@ ", [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ], [NSString stringWithFormat:@"%@", self.searchUserDetailsTextField.text ]];
    [fetchRequest setPredicate:predicate];

    NSLog(@"predicate is %@", predicate);
    NSLog(@"fetchrequest is %@", fetchRequest);
    // fetchrequest is { }

   NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:NO];
   NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
   [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    NSLog(@"count here is %d", [[aFetchedResultsController fetchedObjects]count]);
    // count returned 0

    self.fetchedResultsController = aFetchedResultsController;
    self.fetchedResultsController.delegate = self;
    NSLog(@"count is %d", [[self.fetchedResultsController fetchedObjects]count]);
    // count returned 0

    [self.searchResultsTableView reloadData];
    self.searchResultsTableView.hidden = NO;
}

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.fetchedResultsController fetchedObjects]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSString* displayString = [NSString stringWithFormat:@"%@",[self.fetchedResultsController fetchedObjects]];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", displayString];
    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    detailsViewController = [[ADAssociateDetailsViewController alloc]initWithNibName:@"ADAssociateDetailsViewController" bundle:nil];
    [self.navigationController pushViewController:detailsViewController animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
 }

请帮我解决我的问题。这是我第一次参与NSPredicate。

1 个答案:

答案 0 :(得分:0)

您必须在获取的结果控制器上调用performFetch

NSFetchedResultsController *aFetchedResultsController = ...
NSError *error;
if (![aFetchedResultsController performFetch:&error]) {
    // handle error ...
}
NSLog(@"count here is %d", [[aFetchedResultsController fetchedObjects]count]);