NSPredicate not working

Discussion in 'iOS Programming' started by ppn, Nov 8, 2010.

  1. ppn macrumors member

    Joined:
    Oct 31, 2010
    #1
    Hey Everyone,

    I am trying to create a UITableViewController where one of the functions is to find out how many of each "section" is in my core data. I'm using the NS predicate to set a predicate for my entity as shown below in my countSection method. My sections are A, B, C, D, E and it's only return 1 for A but I have a lot more of each and the other sections are returning 0. I'm noticing if I change my last entry to B then it returns 1 for B. Is there a reason why it's only looking at the last entry instead of the entire database?

    Also, when I try to addObject into my NSMutableArray sectionCountNumber, nothing is being added because when I display the "temp", it's showing (null).

    At the end of the loop, I'm getting this error: -[NSCFString stringValue]: unrecognized selector sent to instance 0xb298.

    Code:
    - (void)countSection {
    	for (int i = 0; i<[lessonSection count]; i++) {
    		NSFetchRequest *request =[[NSFetchRequest alloc] init];
    		if (self.managedObjectContext_ == nil) {
    			self.managedObjectContext_ = [(OntarioG1TestAppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
    		}
    		NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quiz"
    												  inManagedObjectContext:managedObjectContext_];
    		[request setEntity:entity];
    		NSString *sectionName = [lessonSection objectAtIndex:i];
    		NSLog(@"%@", sectionName);
    		NSPredicate *predicate = [NSPredicate predicateWithFormat:@"section==%@", sectionName];
    		[request setPredicate:predicate];
    		NSError *error;
    		// the sections are: 
    		sectionCountNumber = [managedObjectContext_ countForFetchRequest:request error:&error];
    		NSLog(@"%i", sectionCountNumber);
    		[sectionCount addObject:[NSNumber numberWithInteger:sectionCountNumber]];
    		NSString *temp = [sectionCount objectAtIndex:i];
    		NSLog(@"%@", temp);
    		[request release];
    	}
    }
    Here's the complete LessonTableView.h code:

    Code:
    @protocol LessonTableViewDelegate;
    
    @interface LessonTableView : UITableViewController {
    	id <LessonTableViewDelegate> delegate;
    	IBOutlet UIImageView *image;
    	IBOutlet UILabel *question;
    	IBOutlet UILabel *answerA;
    	IBOutlet UILabel *answerB;
    	IBOutlet UILabel *answerC;
    	IBOutlet UILabel *answerD;
    	IBOutlet UIButton *showAnswer;
    	IBOutlet UIButton *next;
    	NSInteger qid;
    	NSInteger counter;
    	NSInteger sectionCountNumber;
    	NSManagedObjectContext *managedObjectContext_;
    	NSMutableArray *lessons;
    	NSMutableArray *lessonSection;
    	NSMutableArray *sectionCount;
    	//Quiz *currentLesson;
    
    }
    
    @property (nonatomic, assign) id <LessonTableViewDelegate> delegate;
    @property (nonatomic, retain) UIImageView *image;
    @property (nonatomic, retain) UILabel *question, *answerA, *answerB, *answerC, *answerD;
    @property (nonatomic, retain) UIButton *showerAnswer, *next;
    @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext_;
    @property (nonatomic, retain) NSMutableArray *lessons, *lessonSection;
    @property (nonatomic, retain) NSMutableArray *sectionCount;
    @property (nonatomic, assign) NSInteger qid;
    @property (nonatomic, assign) NSInteger counter;
    @property (nonatomic, assign) NSInteger sectionCountNumber;
    
    -(void)countSection;
    
    @end
    
    @protocol LessonTableViewDelegate
    - (void)lessonTableViewDidFinish:(LessonTableView *)lesson;
    @end
    Here's my complete LessonTableView.m file:

    Code:
    
    @implementation LessonTableView
    @synthesize delegate;
    @synthesize image;
    @synthesize question, answerA, answerB, answerC, answerD;
    @synthesize showerAnswer, next;
    @synthesize lessons, lessonSection;
    @synthesize sectionCount;
    @synthesize managedObjectContext_;
    @synthesize qid, counter, sectionCountNumber;
    
    #pragma mark -
    #pragma mark View lifecycle
    
    
    - (void)viewDidLoad {
    	lessonSection = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D", @"E", nil];
    }
    
    - (void)countSection {
    	for (int i = 0; i<[lessonSection count]; i++) {
    		NSFetchRequest *request =[[NSFetchRequest alloc] init];
    		if (self.managedObjectContext_ == nil) {
    			self.managedObjectContext_ = [(OntarioG1TestAppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
    		}
    		NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quiz"
    												  inManagedObjectContext:managedObjectContext_];
    		[request setEntity:entity];
    		NSString *sectionName = [lessonSection objectAtIndex:i];
    		NSLog(@"%@", sectionName);
    		NSPredicate *predicate = [NSPredicate predicateWithFormat:@"section==%@", sectionName];
    		[request setPredicate:predicate];
    		NSError *error;
    		// the sections are: 
    		sectionCountNumber = [managedObjectContext_ countForFetchRequest:request error:&error];
    		NSLog(@"%i", sectionCountNumber);
    		[sectionCount addObject:[NSNumber numberWithInteger:sectionCountNumber]];
    		NSString *temp = [sectionCount objectAtIndex:i];
    		NSLog(@"%@", temp);
    		[request release];
    	}
    }
    
    /*
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    }
    */
    /*
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    }
    */
    /*
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    }
    */
    /*
    - (void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
    }
    */
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */
    
    
    #pragma mark -
    #pragma mark Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return [lessonSection count];
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [[sectionCount objectAtIndex:section] intValue];
    }
    
    - (NSString *)tableView:(UITableView *)tableView
    titleForHeaderInSection:(NSInteger)section {
    	NSString *temp = [[lessonSection objectAtIndex:section] stringValue];
    	NSLog(@"%@", temp);
    	return [[lessonSection objectAtIndex:section] stringValue];
    }
    
    
    
    // Customize the appearance of table view cells.
    - (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] autorelease];
        }
        
        // Configure the cell...
        
        return cell;
    }
    
    
    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */
    
    
    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }   
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }   
    }
    */
    
    
    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    }
    */
    
    
    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */
    
    
    #pragma mark -
    #pragma mark Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // Navigation logic may go here. Create and push another view controller.
    	/*
    	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
    	 [self.navigationController pushViewController:detailViewController animated:YES];
    	 [detailViewController release];
    	 */
    }
    
    
    #pragma mark -
    #pragma mark Memory management
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Relinquish ownership any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        // For example: self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
    	[lessons release];
    	[image release];
    	[question release];
    	[answerA release];
    	[answerB release];
    	[answerC release];
    	[answerD release];
    	[managedObjectContext_ release];
    	[lessonSection release];
    	[sectionCount release];
        [super dealloc];
    }
    
    
    @end
    
    
     
  2. ppn thread starter macrumors member

    Joined:
    Oct 31, 2010
    #2
    this is my Quiz Entity in Core Data

    Code:
    @property (nonatomic, retain) NSNumber * status;
    @property (nonatomic, retain) NSNumber * qid;
    @property (nonatomic, retain) NSString * answerD;
    @property (nonatomic, retain) NSString * answerC;
    @property (nonatomic, retain) NSString * answerB;
    @property (nonatomic, retain) NSString * question;
    @property (nonatomic, retain) NSString * answerA;
    @property (nonatomic, retain) NSString * section;
    @property (nonatomic, retain) NSNumber * solution;
    @property (nonatomic, retain) NSString * images;
     
  3. ppn thread starter macrumors member

    Joined:
    Oct 31, 2010
    #3
    I've initialized my section count and it's not returning null anymore but I still can't get my predicate to work. I've modified my countSection method and added a currentLesson as follows:

    Code:
    - (Quiz *) currentLesson {
    	if (self.counter < 0) {
    		return nil;
    	} else {
    		Quiz *lesson = [self.lessons objectAtIndex:(self.counter)];
    		return lesson;
    	}
    }
    
    - (void)countSection {
    	NSFetchRequest *request =[[NSFetchRequest alloc] init];
    	if (self.managedObjectContext_ == nil) {
    		self.managedObjectContext_ = [(OntarioG1TestAppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
    	}
    	NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quiz"
    											  inManagedObjectContext:managedObjectContext_];
    	[request setEntity:entity];
    	NSError *error;
    	self.lessons = [[managedObjectContext_ executeFetchRequest:request error:&error] mutableCopy];
    	[request release];
    	for (int i = 0; i<[lessons count]; i++) {
    		counter = i;
    		NSLog(@"%@", self.currentLesson.topic);
    	}
    	for (int i = 0; i<[lessonSection count]; i++) {
    		NSString *sectionName = [lessonSection objectAtIndex:i];
    		NSLog(@"Section Name %@", sectionName);
    		NSPredicate *predicate = [NSPredicate predicateWithFormat:@"topic==%@", sectionName];
    		NSMutableArray *tempArray = [[self.lessons filteredArrayUsingPredicate:predicate] mutableCopy];
    		NSLog(@"Array Count %i", [tempArray count]);
    		//sectionCountNumber = [managedObjectContext_ countForFetchRequest:request error:&error];
    //		NSLog(@"%i", sectionCountNumber);
    		[sectionCount addObject:[NSNumber numberWithInteger:[tempArray count]]];
    
    	}
    	
    }
    For the first loop that goes through everything in the Array and gives the following
    PHP:
    A
    A
    A
    A
    A
    B
    B
    B
    B
    B
    B
    C
    C
    C
    C
    C
    C
    D
    D
    D
    D
    D
    D
    D
    D
    but when I apply the predicate to the array, I get the following:

    PHP:
    Section Name A
    Array Count 0
    Section Name B
    Array Count 0
    Section Name C
    Array Count 0
    Section Name D
    Array Count 1
    Section Name E
    Array Count 0
    I don't understand why it's only picking up 1 for D and nothing else for the other letters.
     

Share This Page