2013-07-04 110 views
0

我的项目简介:某些数据(所有静态数据)存储在核心数据中。该应用程序有一个文本字段进行搜索。我正在使用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

,请不要使用'[的NSString stringWithFormat:@“%@”'只传递一个字符串参数 - 这已经是一个字符串... – Wain

+0

好的......但是这个问题呢? –

+2

它不应该阻止它的工作。 FRC实际上是否创建?你的商务部实际上是一个有效的参考?你做了什么调试? – Wain

回答

0

你必须调用获取的成果控制器上performFetch

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