2013-07-04 151 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]);