@interface Week : NSManagedObject {
}
@property (nonatomic, retain) NSNumber *weekID;
@property (nonatomic, retain) NSString *top;
@property (nonatomic, retain) NSString *summary1;
@property (nonatomic, retain) NSMutableArray *book;
@end
I'm reading XML file from server and putting vales in above entity.Since Books to be read for that WEEK can be multiple so it is stored in MutableArray.I made *book to be transformable in my .xcdatamodel file.
My parser looks like this which parse and initialize above Week entity.
if ([currentTag isEqualToString:@"book"]) {
NSString *USGSWebLink = [currentElementValue1 stringByStandardizingPath] ;
[week.book addObject:USGSWebLink];
[week setBook:week.book];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"%@", [error domain]);
}
This is storing fine because I fetch the above value from Coredata and store in NSMutableArray "weekArray"**to be displayed in **UITableView.
My program crashes in below two functions and point of crash toggles between below 2 functions...strange!!!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
**//sometimes program crashes at this point**
return [weekArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row];
**// program crash at the above line.**
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [object valueForKey:@"top"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
*Pls tell me where I'm doing wrong ? Is it problem in the way I'm storing NSMutableArray book in my parser or
NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row];
line
Because my NSLog does not print below indexPath.row line.
My fetch function is:
- (void)fetchRecords {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Week" inManagedObjectContext:appDelegate.managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity]; //NSLog(@"fetchRecords = %@",appDelegate.managedObjectContext );
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"weekID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease] ;
if (!mutableFetchResults) {
}
if (mutableFetchResults == nil) {
NSLog(@"week fetch failed");
}
self.weekArray = mutableFetchResults; NSLog(@"weekArray = %@",weekArray);
[mutableFetchResults release];
[request release];
}