2013-07-04 2 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가 실제로 만들어 졌습니까? 귀하의 MOC는 실제로 유효한 참조입니까? 어떤 디버깅을 했습니까? – Wain

답변

0

당신이 가져온 결과 컨트롤러 performFetch 전화를해야 :

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