2010-10-04 55 views
2
@interface Week : NSManagedObject { 

} 
@property (nonatomic, retain) NSNumber *weekID; 
@property (nonatomic, retain) NSString *top; 
@property (nonatomic, retain) NSString *summary1; 
@property (nonatomic, retain) NSMutableArray *book; 

@end 

我是从服务器读取XML文件,并把瓦莱斯在entity.Since上面的书籍来阅读该周可多所以它被存储在MutableArray.I制造* book可在我的.xcdatamodel文件中转换。应坚持的NSMutableArray这是核心数据实体的财产

我的解析器看起来像这样解析并初始化上面周实体

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]); 
    } 

因为我取从Coredata在的NSMutableArray“weekArray”以上的值和存储这是存储细**显示在**的UITableView

我的程序在下面两个函数中崩溃,崩溃点在下面两个函数之间切换......奇怪!

- (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; 
} 

*请告诉我我在哪里做错了?难道是我存储的NSMutableArray 书在我的解析器或 NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row]; 线 因为我的NSLog不会打印下面indexPath.row线的方式问题。

我取功能是

- (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]; 
} 

回答

0

看起来它可能是一个 '过度释放错误'。在你获取方法您有:

NSMutableArray *mutableFetchResults = 
    [[[managedObjectContext executeFetchRequest:request 
              error:&error] mutableCopy] autorelease] ; 

这与初始化的获取结果的可变副本“mutableFetchResults”阵列。然后指定这个数组应该是自动释放的(意味着它将在可能时被释放)。

然后,指定“mutableFetchResults”到您的类与属性:

self.weekArray = mutableFetchResults; 

正如你已经声明此属性为“保留”,当你指定“mutableFetchResults”这个属性的类将保留它。现在别的东西已经通过保留它来声明这个数组的所有权,早期的'autorelease'可以自由发送'release'消息。在这一点上一切都很好,因为你所有的保留和发布都是平衡的。然而,在下一行你明确地再次释放它:

[mutableFetchResults release]; 

这是不必要的,因为它会已经被“自动释放”释放。这种“过度释放”的结果是,分配给“self.weekArray”的引用现在可能指向内存中的一些其他随机对象 - 这可能是您尝试访问数组时应用程序崩溃的原因。

+0

非常感谢“杰克”。删除“[mutableFetchResults发布]后,我的程序工作得很好;” 。我无法想象你自己。 – Ravi 2010-10-05 05:54:09